7 Replies Latest reply on May 28, 2009 4:43 PM by shintashi

    How do I create a Sequence using Actionscript?

    shintashi Level 1

      Suppose I've got 2 movie clips on the stage. box_mc and triangle_mc

       

      I want to set up a way so that whenever I trigger the event, such as pressing a key (like

      if(Key.isDown(Key.SPACE)){

      //stuff that activates it

      }

       

      it does the following in order, spaced by 1 frame, or 1 second (or a specific number of frames)

      [1] box_mc._rotation = -45;

      [2]triangle_mc._rotation = 45;

      [3]box_mc._rotation = 0;

      [4]triangle_mc._rotation = 0;

       

      if I push the Space key again, it would start from the top (1) and repeat the sequence. The sequence would otherwise stop at 4. Previously I've been using counters inside an onEnterFrame such as counter = counter +1; if counter > n, counter = n; etc. This seems tedious and I'm hoping there's a better way to do this. I do not understand watch and timer functions of actionscript (they never seem to work for whatever project I'm occupied with).

       

      Thanks!

        • 1. Re: How do I create a Sequence using Actionscript?
          kglad Adobe Community Professional & MVP

          you can use setInterval() or setTimeout() to time the execution of code (placed in functions).

          • 2. Re: How do I create a Sequence using Actionscript?
            shintashi Level 1
            setInterval(   function  ,   interval  [,   arg1  ,   arg2  , ...,   argn  ] )

            in the above, what is argument 1, argument 2, and so on all about?

            I see a setInterval(function(){actionscript goes here}, miliseconds );

            but I do not see how to integrate a second and different command at a different time; without something like

            setInterval(function(){box rotation script 1}, 000 ); setInterval(function(){triangle rotation script 1}, 1000 );
            setInterval(function(){box rotation script 2}, 2000 );
            setInterval(function(){triangle rotation script 2}, 3000 );

             

            plus a clearInterval (?).

             

            Apologies for my ignorance, I seem to find the simplest things difficult.

            • 3. Re: How do I create a Sequence using Actionscript?
              kglad Adobe Community Professional & MVP
              function startSequenceF(){
              rotateF(box_mc,-45);
              clearTimeout(rotate1I);
              clearTimeout(rotate2I);
              clearTimeout(rotate3I);
              rotate1I=setTimeout(rotateF,2000,triangle_mc,45)
              rotate2I=setTimeout(rotateF,4000,box_mc,0);
              rotate3I=setTimeout(rotateF,6000,triangle_mc,0);
              }

              function boxF(mc:MovieClip,angle:Number){

              mc._rotation = angle;  // or animate this

              }


               

              • 4. Re: How do I create a Sequence using Actionscript?
                shintashi Level 1

                I am unable to infer what your two functions are doing as there is something I am failing to do that makes them work. I'm going to guess the "do stuff" part is in the

                 

                rotateF(box_mc,-45);
                rotate1I=setTimeout(rotateF,2000,triangle_mc,45)
                rotate2I=setTimeout(rotateF,4000,box_mc,0);

                rotate3I=setTimeout(rotateF,6000,triangle_mc,0);

                 

                section, and the "stop doing stuff" is here,

                 

                clearTimeout(rotate1I);
                clearTimeout(rotate2I);
                clearTimeout(rotate3I);

                 

                but I really don't get this function

                 

                 

                function boxF(mc:MovieClip,angle:Number){

                mc._rotation = angle;  // or animate this

                }

                 

                as nothing happened when I put them in the script. Is there a way of activating or calling out these functions with a button, or more I need to be adding?  I chose the triangle and square sequence because I wanted to understand the basic sequential pattern for moving multiple objects before applying the principle to a kinematics program.

                • 5. Re: How do I create a Sequence using Actionscript?
                  kglad Adobe Community Professional & MVP

                  oops, i changed terminology after i started typing the code when i realized i could use one rotate function instead of one for the box and one for the triangle:

                   


                  function startSequenceF(){
                  rotateF(box_mc,-45);
                  clearTimeout(rotate1I);
                  clearTimeout(rotate2I);
                  clearTimeout(rotate3I);
                  rotate1I=setTimeout(rotateF,2000,triangle_mc,45)
                  rotate2I=setTimeout(rotateF,4000,box_mc,0);
                  rotate3I=setTimeout(rotateF,6000,triangle_mc,0);
                  }

                  function rotateF(mc:MovieClip,angle:Number){

                  mc._rotation = angle;  // or animate this

                  }


                   

                  // and to start everything going use:

                   

                  startSequenceF()

                  1 person found this helpful
                  • 6. Re: How do I create a Sequence using Actionscript?
                    shintashi Level 1

                    it definitely worked this time. I edited it for gear appearances and it looks ok:

                     

                    function startSequenceF(){
                    rotateF(box_mc,-45);
                    clearTimeout(rotate1I);
                    clearTimeout(rotate2I);
                    clearTimeout(rotate3I);
                    clearTimeout(rotate4I);
                    clearTimeout(rotate5I);
                    clearTimeout(rotate6I);
                    clearTimeout(rotate7I);
                    rotate1I=setTimeout(rotateF,200,box_mc.triangle_mc,30)
                    rotate2I=setTimeout(rotateF,400,box_mc,-90);
                    rotate3I=setTimeout(rotateF,600,box_mc.triangle_mc,60);
                    rotate4I=setTimeout(rotateF,800,box_mc,-135);
                    rotate5I=setTimeout(rotateF,1000,box_mc.triangle_mc,90);
                    rotate6I=setTimeout(rotateF,1200,box_mc,-180);
                    rotate7I=setTimeout(rotateF,1400,box_mc.triangle_mc,0);
                    }

                     

                    function rotateF(mc:MovieClip,angle:Number){

                     

                    mc._rotation = angle;  // or animate this

                     

                    }

                     

                     

                     

                    // and to start everything going use:

                     


                    onEnterFrame = function(){
                        if(Key.isDown(Key.SPACE)){
                    startSequenceF();
                        }
                    }

                     

                     


                    my next question would probably be "How can I make these changes more graceful/fluid", or would I have to make several minute changes of a few degrees each?

                    • 7. Re: How do I create a Sequence using Actionscript?
                      kglad Adobe Community Professional & MVP

                      you could animate the rotations by using another loop or tween instance.