var dgSound:Sound = new DogSound();
var lnSound:Sound = new LionSound();
var snSound:Sound = new SnakeSound();
var mySoundArray:Array = new Array(dgSound,lnSound,snSound);
mySoundArray[Math.floor(Math.random()*mySoundArray.length)].play();
這將完成你想要的,但爲了將來的參考,任何引用它的東西,比如「東西」,將被編譯爲一個文本向用戶顯示,如果你想訪問實際變量,那麼不要使用引號。
此外:Math.floor()只是將其中的任何十進制數轉換爲其整數值的樓層值。示例:Math.floor(5.1341) - > 5和Math.floor(23.9) - > 23. Math.random()返回一個介於0和.999999之間的隨機值,並將其乘以該數組使它返回一個隨機的十進制值,該數值位於數組範圍內,然後數學平面將其切割下來以訪問其中一個元素。
例如:如果的Math.random()返回0.8562 ...那麼你將得到:
mySoundArray[Math.floor(.8562*mySoundArray.length)].play();
mySoundArray[Math.floor(.8562*3)].play();
mySoundArray[Math.floor(2.5686)].play();
mySoundArray[2].play();
snSound.play();
0.8562是一個相當高的回報,所以你最終得到的最後的值陣列。 我希望我已經解釋了我給你的代碼,如果你還有其他問題,請回復。