Skip to main content
Inspiring
October 26, 2023
Answered

(AS3) Sort and Sound.play() issues

  • October 26, 2023
  • 1 reply
  • 250 views

There are over 2000 songs in the remote directory.
In this case, might "sound.play()" be executed first before "sort()" completes?
So is there a way to prevent this?
I would greatly appreciate it if you could let me know the sample code.

 

var channel: SoundChannel;
var sound: Sound;
var n:uint;
var dm: String = "http://.../Music/";

var sm: Array = [ Music list of over 2000 songs ];
sm.sort(1);

n = Math.floor(Math.random() * (sm.length-1));
musicPlay(sm[n]);

function musicPlay(url: String): void {
	if (channel) channel.stop();
	n = sm.indexOf(url); 

	url = dm + url + ".mp3";
	sound = new Sound(new URLRequest(url));
	channel = new SoundChannel();
	sound.addEventListener(ProgressEvent.PROGRESS, progressHandler);
	channel = sound.play(0);
}
    This topic has been closed for replies.
    Correct answer kglad

    code after the sort() won't execute until sort completes.  it's easy to confirm:

     

    var a:Array = [];
    var n:int = 100000;
    var alphaS:String = "abcdefghijklmnopqrstuvwxyz"
    var alphaA:Array = alphaS.split("");
     
     
    for(var i:int=0;i<n;i++){
    a.push(alphaA[Math.floor(Math.random()*alphaA.length)]+alphaA[Math.floor(Math.random()*alphaA.length)]+alphaA[Math.floor(Math.random()*alphaA.length)]+alphaA[Math.floor(Math.random()*alphaA.length)]+alphaA[Math.floor(Math.random()*alphaA.length)]);
    }
    trace(a);
    trace("\n\n\n");
    a.sort();
    trace(a);

    1 reply

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    October 26, 2023

    code after the sort() won't execute until sort completes.  it's easy to confirm:

     

    var a:Array = [];
    var n:int = 100000;
    var alphaS:String = "abcdefghijklmnopqrstuvwxyz"
    var alphaA:Array = alphaS.split("");
     
     
    for(var i:int=0;i<n;i++){
    a.push(alphaA[Math.floor(Math.random()*alphaA.length)]+alphaA[Math.floor(Math.random()*alphaA.length)]+alphaA[Math.floor(Math.random()*alphaA.length)]+alphaA[Math.floor(Math.random()*alphaA.length)]+alphaA[Math.floor(Math.random()*alphaA.length)]);
    }
    trace(a);
    trace("\n\n\n");
    a.sort();
    trace(a);
    Inspiring
    October 27, 2023

    I checked and I understand
    thank you sir

    kglad
    Community Expert
    Community Expert
    October 27, 2023

    you're welcome.