2014-02-06 24 views
0

我正在AS3中制作一个游戏,我想在定时器完成时在屏幕上随机添加动画片段。定时器完成后的随机动画片段

例:

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); 
} 

没有错误,但如果影片剪辑刚刚出现,我只能点击。如果我正在等待,并出现第二个,我无法点击其中任何一个。

我已经试过:

_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:一个术语是不确定的,没有属性。 你知道我该怎么做吗? 谢谢!

回答

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]); 
+0

很抱歉,但我得到这个错误:错误#1007:尝试在非构造函数上实例化。你知道为什么吗 ? (这是这一行:_movieClips.push(new _classes [Math.floor(Math.random()* _classes.length)]);) – user2421975

+1

您可能需要在第二个方括号后添加一对空括号()即 - 在行尾:... * _classes.length)]());这些通常会用来传递参数给你的新实例类。另一个问题可能是你如何填充你的_classes数组。不要传入字符串:即不是“movieClip”。你想要movieClip,以便传递一个类而不是一个字符串的名字。让我知道如果这仍然行不通。 – moosefetcher

+0

完美!它是括号。谢谢 ! – user2421975