1

我正在尝试从数组中播放随机声音。这是我正在使用的代码。有任何想法吗?这是行不通的。

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

1 回答 1

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]));
            }
        }
    }
}
于 2012-08-16T08:03:51.433 回答