2012-05-01 39 views
2

假設我有一個數組,數組中的每個項目都有一個相應的庫項目。AS3:基於數組值的AddChild

我想這樣做:

var rando = Math.round(Math.random()*3) 
var myArray = new Array ["ball", "wall", "fall"] 
var i:myArray[rando] = myArray[rando] new myArray[rando]() 
addChild(i) 

但是,這是行不通的。什麼祕密?

謝謝你,

雨果

+0

「這不起作用」 - 如何?錯誤或意外行爲? –

+0

首先,你要放置隨機變量,而不是圍繞它。 'var rando = Math.floor(Math.random()* 3);' –

+2

仔細看看你在做什麼 - 你會期望addChild(「ball」)'工作嗎? – Marty

回答

0

如果你只有幾個選擇,它更容易使用的switch-case。

switch (rando) { 
    case 0: 
     i = new ball(); 
     break; 
    case 1: 
     i = new wall(); 
     break; 
    case 2: 
     i = new fall(); 
     break; 
} 
addChild(i); 

我建議你定義變量i作爲MovieClip,在這種情況下,它可以被實例化爲兩個球,牆壁,掉落。

鑑於球,牆和秋天在庫中出口到動作。

1

好的,所以這裏有一堆問題。

大的一個是var i:myArray[rando] = myArray[rando] new myArray[rando]()不太確定你在這裏要做什麼。

無論如何,我要假設球,牆和秋天是您的圖書館中的影片剪輯的實例名稱。我想你會想要這樣的東西

var rando:int = Math.floor(Math.random()*3); //As the comments point out this should give you a random 
//int between 0 and 2, arrays are 0 indexed so this is what we want if we have 3 items 

現在對於你的數組,你現在把字符串放在那裏。 Flash不知道什麼是「球」等。

嘗試這樣的事情

var myArray:Array = new Array [new ball(), new wall(), new fall()]; //this creates a new instance of your library object and stores it in your array 

現在加入其中之一,以你的舞臺:

addChild(myArray[rando]); //this uses the random number to pull one of the items out of your array 

你想與var i:myArray[rando]做並沒有真正意義是什麼。有沒有類型的myArray [rando]這個插槽應該拿着一個MovieClip

0

只是猜測你有限的信息,但給這一槍。

private function myFunction():void{ 
    var rando = Math.round(Math.random()*3); 
    var myArray= new Array ["ball", "wall", "fall"]; 

} 
private function generateItem(item:String):void{ 
    switch(item){ 
     case "ball" : generateBall(); break; 
     case "wall" : generateWall(); break; 
     case "fall" : generateFall(); break; 
} 

private function generateBall():void{ 
//code to generate ball 
addChild(ball); 
} 

private function generateFall():void{ 
//code to generate fall 
addChild(fall); 
} 

private function generateWall():void{ 
//code to generate wall 
addChild(wall); 
} 
-1

Marty Wallace在getDefinitionByName()的路徑中獲得了很高的評價。他發佈的例子很好,但這個例子完全符合我的要求。

http://www.emanueleferonato.com/2011/03/31/understanding-as3-getdefinitionbyname-for-all-eval-maniacs/

+0

我*真的很驚訝沒有一個答案涵蓋了這一點。 – Marty

+0

我已經添加了完整的答案;你是否熟悉[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)? :) – Marty

4

沒有人感到驚訝這裏提到getDefinitionByName()

下面是一些完整的代碼,讓您的工作例如:

var myArray = ["ball", "wall", "fall"]; 

/** 
* Creates a random instance based on an input array containing class names as Strings. 
* @param The input array containing aforementioned Strings. 
* @return The newly created instance. 
*/ 
function createRandom(typeArray:Array):* 
{ 
    // Select random String from typeArray. 
    var selection:String = typeArray[ int(Math.random() * typeArray.length) ]; 

    // Create instance of relevant class. 
    var Type:Class = getDefinitionByName(selection) as Class; 

    // Return created instance. 
    return new Type(); 
} 


// Randomly create and add instance. 
var instance:DisplayObject = createRandom(myArray); 
addChild(instance); 
0

更改您的arrary行:

var myArray = new Array [ball, wall, fall]; 

這應該工作。:)