Search code examples
actionscript-3animationflashtween

Tweening with actionscript 3


I have been working on this one a while. I have an object in this case a movieClip called "mcBall". I set up two buttons "btnLeft" and "btnRight"with a tween so that the mcBall will ease between the two points smoothly. Works fine, but where it gets glitchy is the the two buttons are still active an if the user clicks on either button of course the ball goes back to the starting point like it's supposed to. My question is this ... What is the best way to have the buttons be de-activated while the "mcBall" object is moving. Would it be best to use a removeEventListener for the buttons and then have it added again. Would it be better to use an if statement like "If (mcBall.x = >=81 || <=469) removeEventListener"? Maybe use the tweenEvent.MOTION_FINISH to set up the eventListener again.

Any help would be greatly appreciated. Thanks

using Flash cs3

In this code I managed to turn off one button so that while the ball is moving it remains inactive but the other is still active. I'm not sure of the placement of the removeEventListener.

import fl.transitions.Tween;
import fl.transitions.easing.*;

function moveBallRight(evt:MouseEvent):void {
    var moveBall:Tween=new Tween(mcBall,"x",Regular.easeOut,80,470,4,true);
    btnRight.removeEventListener(MouseEvent.CLICK,moveBallRight);
    btnLeft.addEventListener(MouseEvent.CLICK,moveBallLeft);

}
btnRight.addEventListener(MouseEvent.CLICK,moveBallRight);


function moveBallLeft(evt:MouseEvent) {
    var moveBall:Tween=new Tween(mcBall,"x",Regular.easeOut,470,80,4,true);
    btnRight.addEventListener(MouseEvent.CLICK,moveBallRight);
    btnLeft.removeEventListener(MouseEvent.CLICK,moveBallLeft);


}

btnLeft.addEventListener(MouseEvent.CLICK,moveBallLeft);

Solution

  • I would recommend not using the native Tween class from fl.transitions.Tween. Its not very good. The industry standard is TweenMax from greensock.

    Using TweenMax it is trivially easy to respond to end-of-tween events. You simply add an onComplete:myhandlerfunction to your tween. An example from your above code would look like this: Instead of

     var moveBall:Tween=new Tween(mcBall,"x",Regular.easeOut,80,470,4,true);
    

    You would have:

    TweenMax.to(mcBall, 4, {x:470, ease:Expo.easeOut, onComplete:onBallMovedLeftComplete};
    

    Hope that helps. And I hope you never have to use those native tween classes again. They are the pits.