• 0

AS3 - Wait for event to finish before continuing


Question

What I am trying to do is use a function for reading a textfile, storing the variables into an array, and then return the array. My problem is that I do not know how to wait for the arrays to be populated before returning them. (see code below)

This is my first attempt at working with Flash/Actionscript 3.0. I understand the async part but I am not sure if/how I can workaround it. Nothing that I have googled has provided me with any help. I need some clarity and am hoping someone here can help provide it! Thanks in advance.

var firstList:Array = getWordsArrayFromTextFile("first.txt");
var secondList:Array = getWordsArrayFromTextFile("second.txt");
var index:Number = Math.round(Math.random() * (firstList.length - 1));

firstword_txt.text = firstList[index];
secondword_txt.text = secondList[index];

function getWordsArrayFromTextFile(loc:String):Array{
 var words:Array = new Array();
 var loader:URLLoader = new URLLoader();

 loader.dataFormat = URLLoaderDataFormat.VARIABLES;
 loader.addEventListener(Event.COMPLETE, loading);
 loader.load(new URLRequest(loc));

 function loading (e:Event):void {
  for(var c:Number = 0; c < loader.data.length; c++){
    words.push(loader.data["var_" + c.toString()]);
    }
 }
 return words;  //Returns before complete event has finished, so words array is empty
}

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

aberflasm,

Check this out.

http://www.willcock.org/teaching/flash/adv...AS3)/index.html

This guy is loading XML so you're not going to need some of it, but... Check out what he's doing by using a separate .as file and class. Also notice the use of dispatchEvent.

"dispatchEvent(new Event(XMLParser.PARSING_DONE));"

listened for by..

"xmlMan.addEventListener(XMLParser.PARSING_DONE,parsingDoneListener); "

Doing it this way, you can populate your "firstList" array and then call any functions that make use of that array after the text file has finished loading and not before.

Hope that's helpful.

Edited by williamrust
Link to comment
Share on other sites

  • 0

If you're coding on the timeline, something like this could work but it doesn't use return.

var firstList:Array = new Array();	

function getWordsArrayFromTextFile():void {
	trace("trying to load the text file");
	//load your text file 
	var loadedData:Array = new Array("val1", "val2", "val3");
	//"loading" called by your loader event listener
	loading();
	function loading(){
		for(var c:Number = 0; c < loadedData.length; c++){
			firstList.push(loadedData[c]);
		}
		//call a function to continue your script
                allDone();
	}
}

function allDone():void {
	trace("all done! array is ready");
	var index:Number = Math.round(Math.random() * (firstList.length - 1));
	trace("firstList = " + firstList[index]);

}

getWordsArrayFromTextFile();

Seems like there should be a better way to do this.

Edited by williamrust
Link to comment
Share on other sites

  • 0

Thanks Williamrust for both responses. I will definitely look into them. What I ended up doing was using a preloader and a URLLoader for each file. It works but I feel dirty doing it that way.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.