How to stop all child movieclips inside a movieclip in AS3?

廉价感情. 提交于 2019-12-19 11:46:11

问题


I have a movieclip which is a character in a game. Inside this movieclips there are several movieclips containing limbs that has an animation. So do anyone have a suggestion on how to gotoAndStop(1); on all the movieclips that are inside the character without having to call on every limb object manually?

The character movieclip contains a total of 20 movieclips on 4 frames, so I just want to stop all of the movieclips inside the character.


回答1:


If i understand you correctly (ie: stop all movieclips within another movieclip), this should work:

function stopAllClips(mc:MovieClip):void
{
    var n:int = mc.numChildren;
    for (var i:int=0;i<n;i++)
    {
        var clip:MoviceClip = mc.getChildAt(i) as MovieClip;
        if (clip)
            clip.gotoAndStop(1);
    }
}

Just call it like so:

stopAllClips(yourMovieClip);

Where yourMovieClip is the character.


EDIT

As of Flash Player 11.8 / AIR 3.8, there is a built in method for all DisplayObjectContainers called stopAllMovieClips.

commonParent.stopAllMovieClips();

Keep in mind, this will recursively stop all children and grandchildren, unlike the the original answer which only stops the immediate children.




回答2:


Easiest way I think :

// MovieClip propotype function that stop all running clips (current and inside clips)
MovieClip.prototype.stopAllClips = function():void {
    var mc:MovieClip = this;
    var n:int = mc.numChildren;
    mc.gotoAndStop(1);
    for (var i:int=0; i<n; i++) {
        var clip:MovieClip = mc.getChildAt(i) as MovieClip;
        if (clip) {
            clip.gotoAndStop(1);
            clip.stopAllClips();
        }
    }
}

So it's recursive, and can be called from a MovieClip it-self:

myMovieClip.stopAllClips(); // Stop the clip and inner clips

EDIT

As of Flash Player 11.8 / AIR 3.8, there is a built in method for all DisplayObjectContainers called stopAllMovieClips.

commonParent.stopAllMovieClips();


来源:https://stackoverflow.com/questions/14896655/how-to-stop-all-child-movieclips-inside-a-movieclip-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!