Avg. Rating 5.0

Problem

Animation takes CPU time and sometimes it's useful to stop/play it. The problem is that MovieClip's in animation can be hardly nested.

Solution

Stop/start MovieClips recursively.

Detailed explanation

Here is the code that stops/plays all MovieClips in hierarchy:

public static function massStop(target:DisplayObjectContainer,
frame:Object = 1):void
{
    if (target is MovieClip)
        MovieClip(target).gotoAndStop(frame);
    var n:int = target.numChildren;
    for (var i:int = 0; i < n; i++)
    {
        var child:DisplayObjectContainer = target.getChildAt(i) 
            as DisplayObjectContainer;
        if (child)
            massStop(child, frame);
    }
}

public static function massPlay(target:DisplayObjectContainer):void
{
    if (target is MovieClip)
        MovieClip(target).play();
    var n:int = target.numChildren;
    for (var i:int = 0; i < n; i++)
    {
        var child:DisplayObjectContainer = target.getChildAt(i) 
            as DisplayObjectContainer;
        if (child)
            massPlay(child);
    }
}

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes