2011-04-26 52 views
0

我有一个Flash AS2网站,我需要得到一个instancied影片剪辑内的所有按钮(定义为每一个特定的属性)。我一直在寻找一个小时或更长时间,但我只是得到了AS3的解决方案!有人能帮助我吗?AS2:遍历影片剪辑元件

谢谢大家!

回答

0

您先使用获得的影片剪辑的深度:

var depth:int = movieclip.getDepth(); 

然后你只是通过影片剪辑循环使用一个for循环,直到你达到你getDepth获取的价值:

for(var i:int = 0; i < depth; i++){ 
    trace(movieclip.getInstanceAtDepth(i)); 
} 

这将让影片剪辑中的所有实例。

有一个了解更多关于在参考方法的情况下,因为它没有经过测试,你不能把这个代码的工作。

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/2/help.html?content=00001301.html

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/2/help.html?content=00001302.html

+0

嗨!感谢您的回复和参考。该movieclip.getDepth()返回-1381。如果我使用-1360从-1381进行循环来查看trace()的行为,我得到了一些定义的元素和其他未定义的元素。还有另一种方法可以用较少的迭代来解决这个问题? – Pedro 2011-04-26 22:13:18

2

哇,AS2,还没有看到在一段时间。

在运行时(通过代码)创建的AS2剪辑具有正深度值,并且在authortime(通过“转换为符号”)创建的剪辑具有负深度值。

通过使用for...in循环的最简单方法。 这里是一个包裹在一个很好的可重复使用的功能,这也允许通过目标剪辑内的所有嵌套剪辑任选循环的例子:

var clips:Array = getChildrenOf(this,true); 
var numClips:Number = clips.length; 
for(var i:Number = 0 ; i < numClips ; i++) trace("clip["+i+"]: " + clips[i]._name + " at depth " + clips[i].getDepth() + " in " + clips[i]._parent._name); 

function getChildrenOf(target:MovieClip,recursive:Boolean):Array{ 
    var result:Array = []; 
    for(var i in target){//loop through all properties 
     if(target[i] instanceof MovieClip) {//look for movieclips 
      result.push(target[i]);//found a clip add it to the result array 
      if(recursive) result = result.concat(getChildrenOf(target[i],true));//concatenate children of clips at this level,recurse 
     } 
    } 
    return result; 
} 

第二paramenter是可选的,所以如果忽略它(如getChildrenOf(本);),你只能得到在深度1级的孩子你的目标影片剪辑中(例如,它的孩子,而不是它的“孙子”)

HTH