Search code examples
flashactionscript-3flash-cs4tween

In ActionScript, how to make something move somewhere and move back?


Using Tween class maybe? I tried the easeOut. But if will write 2 Tween, the 2nd one will overwrite the 1st one, so I only see the obj moving in the 2nd Tween direction, not the 1st Tween direction.

I know the coordinates for the 2nd Tween below is not correct (because all coordinates should follow the defined reference point), so I need to find out the logo's width and height. But is alright now because it is for testing purpose.

import fl.transitions.Tween;
import fl.transitions.easing.*;
logo.visible = false;
addChild(logo);

circle.addEventListener(MouseEvent.CLICK, moveObj);

function moveObj(e:MouseEvent):void{
    logo.visible = true;
    var tweenRight:Tween = new Tween(logo,"x",None.easeOut, 100, 300, 2, true);
    var tweenLeft:Tween = new Tween(logo,"x",None.easeOut, 300, 100, 2, true);

}

Solution

  • You are firing the two tweens at the same time so you can for example listen to motion finish event and launch the other tween at this moment:

    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import fl.transitions.TweenEvent;
    
    logo.visible = false;
    addChild(logo);
    
    circle.addEventListener(MouseEvent.CLICK, moveObj);
    
    function moveObj(e:MouseEvent):void{
        logo.visible = true;
        var tweenRight:Tween = new Tween(logo,"x",None.easeOut, 100, 300, 2, true);
        tweenRight.addEventListener(TweenEvent.MOTION_FINISH, onTweenRightFinished);
    }
    function onTweenRightFinished(e:TweenEvent):void {
        e.currentTarget.removeEventListener(TweenEvent.MOTION_FINISH, onTweenRightFinished);
        var tweenLeft:Tween = new Tween(logo,"x",None.easeOut, 300, 100, 2, true);
    }