AS3: Stopping All Timeline Animations

February 5th, 2009 | by Marc |

I haven't posted in months for good reason. I've been involved in a pretty large project at Almighty that led to many late nights. All in all though, the end product turned out to be pretty awesome. I'll post something more on it later. Until then, I hope to roll out some snippets of code that I found useful during the project.

The following snippet of code is a recursive loop that halts all timeline-based animations (assuming they're on a gotoAndStop() loop. This came in handy when working with animation designers.

function haltAllAnimations( mc:MovieClip = null ):void
{
    if( !mc ) mc = this as MovieClip;
 
    for(var i:int = 0; i < mc.numChildren ; i++)
    {
         if( (mc.getChildAt( i ) is MovieClip) == false ) continue;
 
        MovieClip( mc.getChildAt( i ) ).stop();
        if( mc.numChildren > 0 )
            haltAllAnimations( MovieClip( mc.getChildAt( i ) ) );
    }
}
  1. 3 Responses to “AS3: Stopping All Timeline Animations”

  2. Useful, but not perfect. What if you pass Sprite instance to haltAllAnimations method? Sprites can have MovieClips as children, so your solution will not work. Try this one:

    function stopAllChildMovieClips(displayObject:DisplayObjectContainer):void {
    var numChildren:int = displayObject.numChildren;
    for (var i:int = 0; i < numChildren; i++) {
    var child:DisplayObject = displayObject.getChildAt(i);
    if (child is DisplayObjectContainer) {
    if (child is MovieClip) {
    MovieClip(child).stop();
    }
    stopAllChildMovieClips(DisplayObjectContainer(child));
    }
    }
    }

    Mike on Mar 24, 2009 | Reply

  3. I approved this comment but never actually commented on it back… sorry … But yeah, Mike, that’s a much better way of including everything. I didn’t think about Sprites. Thank you for the contribution!

    Marc on May 14, 2009 | Reply

  4. Used the reply by Mike, worked great. I passed the stage as the displayObject, and everything stopped. Problem came up where I couldn’t resume play on the nested timeline, but the main timeline did resume. To resume play, I basically copied the function above, and changed stop to play.

    pezzomon on Sep 9, 2009 | Reply

Post a Comment