11 Replies Latest reply on Mar 14, 2011 1:56 PM by mdermez

    Speed up a tween (CS4 AS3)

    mdermez Level 1

      I have an animated fish crossing the screen. I made that using the animated fish (movieclip1) following a path on a guide layer. All these are into another movieclip (2).

      I want in mouse hover condition to accelerate the tween. In other words I want the user to point the fish with the mouse (mouse hover) and then the fish tries to escape (tween acceleration). How can I accelerate the tween?

      Keep in mind that in fish movieclip(1) I have more than one Tweens. So the code must be for selected tween (named by frame label).

      Another thing is that acceleration mustn't be directly from 0 to 1. There must be an ease on the start and on the end of acceleration so it can be more realistic.

      I am not related on programming so I would like a complete answer (not just directions)

       

      My thinking of the code was:

       

      (if it's needed I'll have to import transitions.ease and tween)

       

      Function for mouse hover condition

      --> inside that function I have to define (I think) another function about acceleration of the tween

       

      Then I have to add eventListener  for the object.

       

      So my main question for the code is focused on tween acceleration

        • 1. Re: Speed up a tween (CS4 AS3)
          kglad Adobe Community Professional & MVP

          if your tween is on a movieclip timeline, the tween will play faster if you play the movieclip timeline faster.  you can do that, within certain limits, by using a timer instance's listener function to advance the movieclip's timeline.

          1 person found this helpful
          • 2. Re: Speed up a tween (CS4 AS3)
            mdermez Level 1

            I find it very useful. Could you give me an example?

            • 3. Re: Speed up a tween (CS4 AS3)
              kglad Adobe Community Professional & MVP

              var mc_playrateChange:Movieclip;

               

              framerateF(mc,32,1,mc.totalFrames);

               

              ///   don't change below ///

              function framerateF(mc:MovieClip,framerate:int,startframe:int,endframe:int):void{

              mc_playrateChange = mc;

              mc.timer=new Timer(1000/framerate,endframe-startframe);

              mc.gotoAndStop(startframe);

              mc.timer.addEventListener(TimerEvent.TIMER,playF);

              mc.timer.addEventListener(TimerEvent.TIMER_COMPLETE,playcompleteF);

              mc.timer.start();

              }

              function playF(e:TimerEvent):void{

              mc_playrateChange.nextFrame();

              }

              function playcompleteF(e:TimerEvent):void{

              mc_playrateChange.timer.removeEventListener(TimerEvent.TIMER,playF);

              mc_playrateChange.timer.removeEventListener(TimerEvent.TIMER_COMPLETE,playcompleteF);

              }

              1 person found this helpful
              • 4. Re: Speed up a tween (CS4 AS3)
                mdermez Level 1

                Ok let me see if I got it right. (thanks for the code)

                 

                lets say that I have a movieClip named "thatBall". Inside that clip there is a tween for the ball. The tween is 200 frames from 1 to 200.

                 

                And also lets say that I want the speed up to run in a mouse click condition. The button will be named "btn1"

                 

                var mc_playrateChange:Movieclip;

                 

                framerateF(mc,32,1,mc.totalFrames); ///What is 32,1??

                 

                function framerateF(mc:MovieClip,framerate:int,startframe:int,endframe:int):void{

                mc_playrateChange = mc;

                mc.timer=new Timer(1000/framerate,endframe-startframe); ///endframe-startframe its a variable

                mc.gotoAndStop(startframe); /////you mean mc.gotoAndStop(1); ?

                mc.timer.addEventListener(TimerEvent.TIMER,playF);

                mc.timer.addEventListener(TimerEvent.TIMER_COMPLETE,playcompleteF);

                mc.timer.start();

                }

                function playF(e:TimerEvent):void{

                mc_playrateChange.nextFrame();

                }

                function playcompleteF(e:TimerEvent):void{

                mc_playrateChange.timer.removeEventListener(TimerEvent.TIMER,playF);

                mc_playrateChange.timer.removeEventListener(TimerEvent.TIMER_COMPLETE, playcompleteF);

                }

                 

                 

                btn1.addEventListener(MouseEvent.CLICK,???) ////what function should I put here in order to activate the tween acceleration?

                • 5. Re: Speed up a tween (CS4 AS3)
                  kglad Adobe Community Professional & MVP

                   

                  var mc_playrateChange:Movieclip;

                   

                   

                   

                  function framerateF(mc:MovieClip,framerate:int,startframe:int,endframe:int):vo id{

                  mc_playrateChange = mc;

                  mc.timer=new Timer(1000/framerate,endframe-startframe); ///endframe-startframe its a variable

                  mc.gotoAndStop(startframe); /////you mean mc.gotoAndStop(1); ?

                  mc.timer.addEventListener(TimerEvent.TIMER,playF);

                  mc.timer.addEventListener(TimerEvent.TIMER_COMPLETE,playcompleteF);

                  mc.timer.start();

                  }

                  function playF(e:TimerEvent):void{

                  mc_playrateChange.nextFrame();

                  }

                  function playcompleteF(e:TimerEvent):void{

                  mc_playrateChange.timer.removeEventListener(TimerEvent.TIMER,playF);

                  mc_playrateChange.timer.removeEventListener(TimerEvent.TIMER_COMPLETE, playcompleteF);

                  }

                   

                   

                  btn1.addEventListener(MouseEvent.CLICK,f);

                   

                  function f(e:MouseEvent):void{

                  framerateF(thatBall,32,1,200); ///What is 32,1 -> framerate and startframe

                  }

                  • 6. Re: Speed up a tween (CS4 AS3)
                    mdermez Level 1

                    var mc_playrateChange:Movieclip;

                     

                    framerateF(thatBall,32,1,200);

                     

                    function framerateF(thatBall:MovieClip,framerate:int,startframe:int,endframe:int):vo id{

                    mc_playrateChange = mc;

                    mc.timer=new Timer(1000/32,200-1); ////200-1?? is this right?

                    thatBall.gotoAndStop(1);

                    thatBall.timer.addEventListener(TimerEvent.TIMER,playF);

                    thatBall.timer.addEventListener(TimerEvent.TIMER_COMPLETE,playcompleteF);

                    thatBall.timer.start();

                    }

                    function playF(e:TimerEvent):void{

                    mc_playrateChange.nextFrame(); //// must be thatBall_playrateChange.nextFrame(); or it doesnt matter?

                    }

                    function playcompleteF(e:TimerEvent):void{

                    mc_playrateChange.timer.removeEventListener(TimerEvent.TIMER,playF);

                    mc_playrateChange.timer.removeEventListener(TimerEvent.TIMER_COMPLETE, playcompleteF);

                    }

                     

                     

                    ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

                    btn1.addEventListener(MouseEvent.CLICK,???) ////what function should I put here in order to activate the tween acceleration?

                     

                     

                     

                    Again, thanks for your time!!! I really apreciate it!!!!

                     

                     

                    • 7. Re: Speed up a tween (CS4 AS3)
                      kglad Adobe Community Professional & MVP

                      don't change the code in framerateF(), playF() or playcompleteF().

                       

                      to speed thatBall, copy and paste the code i suggested.  you can change the frame rate in your mouse listener function:

                       

                      function f(e:MouseEvent):void{

                      framerateF(thatBall,50,1,200);  //play thatBall at ~ 50fps from frame 1 to frame 200

                      }

                      • 8. Re: Speed up a tween (CS4 AS3)
                        mdermez Level 1

                        Ok my bad. I just got the whole thing.

                         

                        I have one more question (last I hope). I still dont get it with the btn1 listener.

                         

                        function SpeedUP(e:MouseEvent):void{

                        framerateF(thatBall,50,1,200);

                        }

                         

                        so the btn1 listener should be something like this? :

                        btn1.addEventListener(MouseEvent.CLICK,SpeedUP)

                        • 9. Re: Speed up a tween (CS4 AS3)
                          mdermez Level 1

                          I got this error:

                          1046: Type was not found or was not a compile-time constant: Movieclip.  var mc_playrateChange:Movieclip;

                          • 10. Re: Speed up a tween (CS4 AS3)
                            kglad Adobe Community Professional & MVP

                            that's a typo.  it should be:

                             

                            MovieClip

                             

                            not

                             

                            Movieclip

                            • 11. Re: Speed up a tween (CS4 AS3)
                              mdermez Level 1

                              Your code worked almost fine - the thing is that is worked.

                               

                              I changed the startframe with the currentFrame command in order to run the function from the frame that its triggered. I dont wont to put limits on startframe by defining it into the code.

                               

                              The clip starts playing normally. Normal speed. In about the middle I want to change it speed. So (according to the code)  I just have to place the cursor on the clip in order to trigger the function (it could be with the clip of a button). The thing is that everytime that the function is triggered the clip starts -not from the frame that it was triggered- but from the begggining.

                               

                              Do you understant what I am saying? Sorry for my english!!

                               

                              thatBall.play();

                              var mc_playrateChange:MovieClip;

                              function framerateF(mc:MovieClip,framerate:int,currentFrame:int,endframe:int):void{
                              mc_playrateChange = mc;
                              mc.timer=new Timer(1000/framerate,endframe-currentFrame);
                              mc.gotoAndStop(currentFrame);
                              mc.timer.addEventListener(TimerEvent.TIMER,playF);
                              mc.timer.addEventListener(TimerEvent.TIMER_COMPLETE,playcompleteF);
                              mc.timer.start();
                              }

                              function playF(e:TimerEvent):void{
                              mc_playrateChange.nextFrame();
                              }

                              function playcompleteF(e:TimerEvent):void{
                              mc_playrateChange.timer.removeEventListener(TimerEvent.TIMER,playF);
                              mc_playrateChange.timer.removeEventListener(TimerEvent.TIMER_COMPLETE, playcompleteF);
                              }

                              function SpeedUp(e:MouseEvent):void{
                              framerateF(thatBall,50,currentFrame,400)
                              }

                              function SpeedDown(e:MouseEvent):void{
                              framerateF(thatBall,30,currentFrame,400)
                              }

                              thatBall.addEventListener(MouseEvent.MOUSE_OVER, SpeedUp);
                              thatBall.addEventListener(MouseEvent.MOUSE_OUT, SpeedDown);

                              ///function mouseOverHandler(e:Event):void{
                                 /// addEventListener(Event.ENTER_FRAME, SpeedUp);
                              ///}

                              ///function mouseOutHandler(e:Event):void{
                                 /// removeEventListener(Event.ENTER_FRAME, SpeedUp);
                              ///}

                              ///thatBall.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
                              ///thatBall.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);