as3 URLRequest声音存储在数组中?

时间:2012-08-16 07:50:09

标签: arrays actionscript-3 audio

我正在尝试播放阵列中的随机声音。这是我正在使用的代码。有任何想法吗?这不起作用。

 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();

1 个答案:

答案 0 :(得分: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]));
            }
        }
    }
}
相关问题