计时器完成时随机movieClips

时间:2014-02-06 10:50:54

标签: actionscript-3 timer

我正在AS3中制作游戏,我想在计时器完成时随机在屏幕上添加movieClip。

例如:

something.addEventListener(TimerEvent.TIMER_COMPLETE, Finish);

function Finish(event : TimerEvent) : void {
randomly add movieClip1 or movieClip2 or movieClip3
}

我该怎么做?

非常感谢你。


修改

感谢您的回答。我尝试过很多东西,但没有什么真正有效......我试过了:

_movieClips.push(new _classes[Math.floor(Math.random() * _classes.length)]()); // this line chooses a random index of your _classes Array which will return the Class at that index 
stageRef.addChild(_movieClips[_movieClips.length-1]); 
if (stageRef.getChildByName("_movieClips[0]") == null) { 
trace("poubelle1"); 
_movieClips[0].addEventListener(MouseEvent.CLICK, removePoubelle, false, 0, true); 
}else if (stageRef.getChildByName("_movieClips[1]") == null) { 
trace("poubelle2"); 
_movieClips[1].addEventListener(MouseEvent.CLICK, removePoubelle2, false, 0, true); 
}else if (stageRef.getChildByName("_movieClips[2]") == null) { 
trace("poubelle3"); 
_movieClips[2].addEventListener(MouseEvent.CLICK, removePoubelle3, false, 0, true); 
}

没有错误,但我只能在movieClip刚刚出现时单击。如果我在等,第二个出现,我就无法点击其中任何一个。

我试过了:

_movieClips.push(new _classes[Math.floor(Math.random() * _classes.length)]()); // this line chooses a random index of your _classes Array which will return the Class at that index 
stageRef.addChild(_movieClips[_movieClips.length-1]); 
if (_movieClips[0].visible== true){ 
trace("poubelle1"); 
_movieClips[0].addEventListener(MouseEvent.CLICK, removePoubelle, false, 0, true); 
} 
if (_movieClips[1].visible== true){ 
trace("poubelle2"); 
_movieClips[1].addEventListener(MouseEvent.CLICK, removePoubelle2, false, 0, true); 
} 
if (_movieClips[2].visible== true){ 
trace("poubelle3"); 
_movieClips[2].addEventListener(MouseEvent.CLICK, removePoubelle3, false, 0, true); 
} 

但错误#1010:术语未定义且没有属性。 你知道我怎么做吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用带有类名称的“加载”数组:

var _classes:Array = new Array(movieClip1, movieClip2, movieClip3);
var _movieClips:Array = new Array(); // this Array is used to instantiate your random MovieClips

然后,在你的计时器完成处理程序:

_movieClips.push(new _classes[Math.floor(Math.random() * _classes.length)]); // this line chooses a random index of your _classes Array which will return the Class at that index
addChild(_movieClips[_movieClips.length-1]);