as3 URLRequest声音存储在数组中?

问题描述:

我想玩数组中的随机声音。这是我正在使用的代码。有任何想法吗?这是行不通的。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

不能使用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