我正在尝试循环使用我的10"炸弹" movieclips并指定一个eventlistener,它调用相同的函数但传递当前的动画片段名称。 "炸弹" movieclip名称递增。
以下是我的尝试
var i:number;
i=0;
while (i <= 10){
var current_bomb:Movieclip = (movingbomb_+i);
current_bomb.addEventListener(MouseEvent.ROLL_OVER, function updateBomb(current_bomb));
i++
}
function updateBomb(currentBomb):void{
currentBomb.gotoAndPlay(2);
}
答案 0 :(得分:1)
关闭,但不完全。此外,使用for
循环是一个更好的主意。通过这些更改,代码应如下所示:
for (var i:int = 0; i < 10; i++) {
var currentBomb:MovieClip = this["movingbomb_" + i];
currentBomb.addEventListener(MouseEvent.ROLL_OVER, function (evt:MouseEvent):void { updateBomb(currentBomb); });
}
function updateBomb(currentBomb:MovieClip):void {
currentBomb.gotoAndPlay(2);
}
这是如何运作的。
for
循环将所有while
循环代码简化为单个语句以提高效率。this[name]
,其中name
是一个字符串。我们使用的字符串将是“movingbomb_”,最后加上i
。updateBomb
函数并传递currentBomb
对象。