Targeting multiple MovieClips which have the same instance name

元气小坏坏 提交于 2019-12-23 05:19:49

问题


On the stage, I have 3 MovieClips who have the same instance name, which is

zeroMC

but all three are an instance of different MovieClips. The first zeroMC is an instance of blank1,the second zeroMC is an instance of blank2 and the third zeroMC is an instance of blank3.

I want to make all three movieclips gotoAndStop at 2, but when I do

zeroMC.gotoAndStop(2);

only one goes to and stops at 2. I also tried

var containers = [zeroMC, zeroMC, zeroMC];

for (var i:int = 0; i<containers.length; i++) {
    containers[i].gotoAndStop(2);
}

but that also only made one zeroMC gotoAndStop at 2. How do I get all three to gotoAndStop at 2?


回答1:


You can only have one reference to an on-stage MovieClip, so you will not be able to update all three as simultaneously as you hope.

I'd recommend storing your MovieClips in an Array and adding the MovieClips to the stage using ActionScript (if you aren't already):

var _movieClips:Array = new Array();

_movieClips.push(new ZeroMC()); // in this case 'ZeroMC' will need to be the Class name of your MovieClip
_movieClips.push(new ZeroMC());
_movieClips.push(new ZeroMC());
for (var loop:int=0;loop<_movieClips.length;loop++) {
    addChild(_movieClips[loop]);
    _movieClips[loop].gotoAndStop(2); // you may want to do this in your game loop, or wherever it is you need your MovieClips to go to frame 2. You will need to LOOP through them though...
}


来源:https://stackoverflow.com/questions/22638987/targeting-multiple-movieclips-which-have-the-same-instance-name

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