2014-02-06 26 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