2012-08-16 32 views
1

我想玩數組中的隨機聲音。這是我正在使用的代碼。有任何想法嗎?這是行不通的。as3 URLRequest聲音存儲在數組中?

import flash.media.Sound; 

//var mySound:Sound = new Sound(); 
var mySoundsArray:Array = ["blue.mp3","green.mp3","red.mp3","yellow.mp3"]; 
var storedSounds:Array; 

for(var i =0; i < mySoundsArray.length; i++) 
{ 
/// DOES NOT WORK BELOW 
storedSounds[i] = new Sound(); 
storedSounds[i].load(new URLRequest("sounds/" + mySoundsArray[i])); 
} 


/// later to loop through sounds but for now I use the line below default at 0 
mySoundsArray[0].play(); 
+0

工作得很好'這不是working'出現任何錯誤? – Kolyunya 2012-08-16 07:54:38

+0

TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。 \t at test_fla :: MainTimeline/frame1() – 2012-08-16 08:02:02

+0

很確定問題在於這一行 - - storedSounds [i] = new Sound(); – 2012-08-16 08:02:39

回答

1

不能使用play方法mySoundsArray元素,因爲它們不健全對象,但字符串。嘗試改變最後一行storedSounds[0].play()

更新

此代碼爲我

package 
{ 
    import flash.display.Sprite; 
    import flash.media.Sound; 
    import flash.net.URLRequest; 

    public class test extends Sprite 
    { 
     private var names:Array = new Array("blue.mp3","green.mp3","red.mp3","yellow.mp3"); 
     private var sounds:Array = new Array(); 

     public function test() 
     { 
      for(var i:uint = 0; i < this.names.length; i++) 
      { 
       sounds[i] = new Sound(new URLRequest("sounds/" + this.names[i])); 
      } 
     } 
    } 
} 
+0

如何將聲音存儲在storedSounds中? - - TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。 \t at test_fla :: MainTimeline/frame1() – 2012-08-16 08:09:19

+0

storedSounds [0]沒有值。 – 2012-08-16 08:15:58

+0

我會這樣做:'var mySoundsArray:Array = new Array(「blue.mp3」,「green.mp3」,「red.mp3」,「yellow.mp3」);' – Kolyunya 2012-08-16 08:21:09