Search code examples
actionscript-3loopsdisplayobjectgsap

Looping DisplayObject animation


I have multiple object doing random movement with random speed. i wanted to repeat the animation.

I tried to use onComplete to restart each object once the animation end, but how could I specifically target it to that object? Currently its overflow

private function lineAnimation (e:DisplayObject):void
    {
        TweenLite.to (e,randomTime, {x:randomX, onComplete: lineAnimation(e)});
    }

Solution

  • When you set a callback for onComplete, you need to just pass the function itself. By setting onComplete: lineAnimation(e), it is executing lineAnimation again over and over. As you need to pass the parameter, you can use an anonymous function for the callback like so:

    private function lineAnimation (e:DisplayObject):void
    {
        TweenLite.to(e, randomTime, {x:randomX, onComplete:function():void {lineAnimation(e)}});
    }
    

    Also since you are using TweenLite, you may want to check out TweenMax which has looping built-in so you can do something like so:

    TweenMax.to(e, randomTime, {x:randomX, repeat:-1}); // -1 repeats indefinitely