as3 for循环无法有效加载代码 - 创建随机mp3播放列表

时间:2012-09-21 18:24:45

标签: arrays actionscript-3 flash for-loop loading

我有这个功能,我将应用程序放在Droid和IOS设备上运行。

目的:它通过10个文件夹,每个文件夹包含mp3。它获取内容并从文件夹中随机选择并将随机mp3放入另一个随机选择的mp3阵列中。最后一个数组是随机播放列表。

大部分时间它都有效但有时第一个Var留空。我想知道第二个数组是否正在尝试获取第一个数组的内容并且cpu运行缓慢所以可能它有时不起作用。

有没有办法让这些代码遵循特定的顺序以确保正常运行?

function loadRandomMeditations(Language:String,CurrentMystery:String,CurrentDecade:String)
    {

    RandomSounds = [];
    TempArray = [];

    sounds = [];
    for(var a:int = 1; 11>a;a++)
        {
            var folder:String = "Languages/" + Language + "/Meditations/" + CurrentMystery + "/"+CurrentDecade+"/"+a+"/";
            var desktop:File = File.applicationDirectory.resolvePath(folder);
            var medSound:Array = desktop.getDirectoryListing();

            /// Looping thru to get all sounds in this directory
            for (var i:uint = 0; i < medSound.length; i++)
                {
                            // puts all sounds of folder into a temporary array
                TempArray.push(medSound[i].url);
                }

                    /// This counts the array and chooses a random number.
            var num = 1 + Math.round(Math.random()*(medSound.length-1));

                    //Adds Zero to make counting even
            TempArray.unshift("Zero");

            /// Puts the randomly selected array item into another array.
            RandomSounds.push(TempArray[num]);

            sounds[a] = new Sound(new URLRequest(TempArray[num]));
            TempArray = [];
        }
    trace(RandomSounds);

}

输出:有时第一个mp3没有加载到最终的播放列表数组中。

1 个答案:

答案 0 :(得分:0)

你复杂了你的功能

function loadRandomMeditations(Language:String,CurrentMystery:String,CurrentDecade:String)
  {
  var medSound:Array = []
  var RandomSounds:Array = [];
  var sounds:Array = [];
  var folder:String = "Languages/" + Language + "/Meditations/" + CurrentMystery + "/"+CurrentDecade+"/";
  var desktop:File;
  var i:int;
  var num:int;
  // stay with the standard and use Lower Case I in a for loop
  for(i = 1; 11>i;i++)
    {
    desktop = File.applicationDirectory.resolvePath(folder+i+"/");
    medSound= desktop.getDirectoryListing();

    // This counts the array and chooses a random number.
    // not sure why you have a 1 + on this but it needs to go
    num = Math.round(Math.random()*(medSound.length-1));

    // why create a second "temp" array when you can just pick one off the medSound array
    // Puts the randomly selected array item into another array.
    RandomSounds.push(medSound[num].url);

    sounds[a] = new Sound(new URLRequest(medSound[num].url));
  }
  trace(RandomSounds);
}