I had a project that loaded individual SWF files for playing in sequence. There were the typical play, pause, previous, next buttons. The SWFs were loaded via Loader. An MP3 file was associated with each SWF.
If the loaded SWF had no nested movieclips, everything went smoothly with movieClip.stop(), movieClip.play(), etc. However on this project, there were nested movieClips in the SWF, so these all needed to be stopped when the user pressed pause, and restarted when the user pressed play.
-
private function stopMovieClipAndChildren(content:DisplayObjectContainer):void
-
{
-
if (content is MovieClip)
-
(content as MovieClip).stop();
-
if (content.numChildren)
-
{
-
var child:DisplayObjectContainer;
-
var n:int = content.numChildren;
-
for (var i:int = 0; i < n; i++)
-
{
-
if (content.getChildAt(i) is DisplayObjectContainer)
-
{
-
child = content.getChildAt(i) as DisplayObjectContainer;
-
if (child.numChildren)
-
stopMovieClipAndChildren(child);
-
else if (child is MovieClip)
-
(child as MovieClip).stop();
-
}
-
}
-
}
-
}
-
private function playMovieClipAndChildren(content:DisplayObjectContainer):void
-
{
-
if (content is MovieClip)
-
{
-
var movieClip:MovieClip = content as MovieClip;
-
if (movieClip.currentFrame < movieClip.totalFrames) // if the main timeline has reached the end, don't play it
-
movieClip.play();
-
}
-
if (content.numChildren)
-
{
-
var child:DisplayObjectContainer;
-
var n:int = content.numChildren;
-
for (var i:int = 0; i < n; i++)
-
{
-
if (content.getChildAt(i) is DisplayObjectContainer)
-
{
-
child = content.getChildAt(i) as DisplayObjectContainer;
-
if (child.numChildren)
-
playMovieClipAndChildren(child);
-
else if (child is MovieClip)
-
{
-
var childMovieClip:MovieClip = child as MovieClip;
-
if (childMovieClip.currentFrame < childMovieClip.totalFrames)
-
childMovieClip.play();
-
}
-
}
-
}
-
}
-
}
(my google search landed me here before writing my own)
Clicking the previous button presented a problem however. Calling movieClip.gotoAndStop(1) on the movieClip (and even all its children) was not putting it back to its “new out of the box” state. I needed a way to do an ultimate reset of the movieClip but since it was a loaded SWF, I didn’t have the class to do:
-
movieClip:MovieClip = new SomeLoadedMovieClip();
However, after reading this blog I found out how.
-
private function resetMovieClip(movieClip:MovieClip):MovieClip
-
{
-
var sourceClass:Class = movieClip.constructor;
-
var resetMovieClip:MovieClip = new sourceClass();
-
return resetMovieClip;
-
}
Use it like this:
-
stopMovieClipAndChildren(movieClip);
-
// remember to remove all event listeners from movieClip and anything that references it
-
// remove movieClip from the display list
-
removeChild(movieClip);
-
// reset the loaded movieClip by creating a new instance of it
-
movieClip = resetMovieClip(movieClip);
-
addChild(movieClip);