5Feb/095
AS3: Stopping All Timeline Animations
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 ) ) ); } }
March 24th, 2009 - 20:44
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));
}
}
}
May 14th, 2009 - 08:09
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!
September 9th, 2009 - 11:55
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.
August 30th, 2010 - 01:39
Very helpful thanks a lot.
June 3rd, 2011 - 18:49
Very useful, thanks!
Unfortunately this doesn’t cover certain child timeline instances. For example, when you have one timeline with some frames that are empty, and some frames that have movieclips that themselves have multiple frames. If it happens to be called when the top timeline is on an empty frame, there is no pointer available to the child timeline available to the code.
I don’t know how to get around this.