Skip to main content
Inspiring
Apr 24, 2023 11:27 AM UTC
Answered

AS3.0 URL Loader and Delay

Download the music list (text file) from the remote directory
I want to make a music list.
After "A codes" have been fully executed, "B codes and below"
Is there any way to run it?
thank you

var n: uint;
var sm:Array = [];
musicList => textField
musicList_btn =>musicList button

//=== A codes =======================================
var request:URLRequest = new URLRequest("http://......./Text/soundList.txt");
var loader:URLLoader = new URLLoader();
loader.load(request);
loader.addEventListener(Event.COMPLETE, completeHandler);

function completeHandler(event:Event):void {
	var str:String = event.target.data;
	sm = str.split(",");  
	sm.sort();
	
	for (var i: uint = 0; i < sm.length; i++) { 
	     musicList.appendText(sm[i]+"\n");
	}
}

//=== B codes ======================================
musicList_btn.addEventListener(MouseEvent.CLICK, musicListSelect);
var musicListOK: Boolean = false;

function musicListSelect(evt: MouseEvent): void {
	musicListOK = !musicListOK;
	if (musicListOK) {
	     musicList.addEventListener(MouseEvent.CLICK, selectMusic);
	} else {
	     musicList.removeEventListener(MouseEvent.CLICK, selectMusic);
	}
}

function selectMusic(e: MouseEvent): void {
	var index: uint = musicList.getLineIndexAtPoint(e.localX, e.localY);
	if (index >= musicList.bottomScrollV) return;
	if ((index != -1) && (index != n)) {
	     n = index;
	     musicPlay(sm[n]);
	}
}

//== next codes =====================
n = int(Math.random() * (sm.length - 1));  //random play
musicPlay(sm[n]);

//=== error ====================
TypeError: Error #2007: Parameter text must be non-null.
	at flash.text::TextField/set text()
	at FiatSound_001_fla::MainTimeline/musicPlay()
	at FiatSound_001_fla::MainTimeline/frame1()

TypeError: Error #1009: Cannot access a property or method of a null object reference.
	at FiatSound_001_fla::MainTimeline/soundCompleteHandler()

 

    This topic is closed to new replies. Start a new post to keep the conversation going.
    Vladin M. Mitov
    Vladin M. MitovCorrect answer
    Inspiring
    Apr 24, 2023 4:09 PM UTC

    Hi,

    I recommend adding trace() statements to several key places in your code to determine the execution sequence. You'll find that the code you want to execute "after" is actually being executed before the completeHandler() function, despite being written further down in the script file.

    Additionally, as a general best practice, I recommend organizing your code into classes and functions. This will help improve code structure and readability, and ensure that the execution sequence is more easily managed and understood.

     

     

    - Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation

    1 reply

    Vladin M. Mitov
    Vladin M. MitovCorrect answer
    Inspiring
    Apr 24, 2023 4:09 PM UTC

    Hi,

    I recommend adding trace() statements to several key places in your code to determine the execution sequence. You'll find that the code you want to execute "after" is actually being executed before the completeHandler() function, despite being written further down in the script file.

    Additionally, as a general best practice, I recommend organizing your code into classes and functions. This will help improve code structure and readability, and ensure that the execution sequence is more easily managed and understood.

     

     

    Translate
    - Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation