0

我怎样才能运行代码然后阵列准备好?

当我运行以下代码时,我收到一条错误消息: TypeError: Error #1010: A term is undefined and has no properties。

import flash.filesystem.File;

var desktop:File = File.applicationDirectory.resolvePath("sounds/drums");

var sounds:Array = desktop.getDirectoryListing();

for (var i:uint = 0; i < sounds.length; i++)
   {
    trace(sounds[i].nativePath); // gets the path of the files
    trace(sounds[i].name);// gets the name
   }


var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("sounds/drums/"+sounds[i].name+""));
myChannel = mySound.play();
4

1 回答 1

2

我通常使用类似下面的东西,每次加载声音时都会存储它并增加一个计数器,一旦加载了所有声音,您就可以调度一个事件或开始播放存储在loadedSounds.

var sounds:Array = desktop.getDirectoryListing();

var loadedSounds:Object = {};
var soundsLoaded:int = 0;

for (var i:uint = 0; i < sounds.length; i++)
{
    var mySound:Sound = new Sound();
    mySound.addEventListener(Event.COMPLETE, onSoundLoaded);
    mySound.load(new URLRequest("sounds/drums/"+sounds[i].name));
}

private function onSoundLoaded(e:Event):void
{
    var loadedSound = e.target as Sound;
    // just get the file name without path and use it as key
    var lastIndex:int = loadedSound.url.lastIndexOf("/");
    var key:String = loadedSound.url.substr(lastIndex+1);

    // store sounds for later reference
    loadedSounds[key] = loadedSound ;

    soundsLoaded++;
    if (soundsLoaded == sounds.length)
    {
        //all sounds loaded, can start playing
    }
}
于 2013-05-18T16:14:02.687 回答