• 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
}

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
  • 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
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • That would be nice, if excel could refresh pivot tables without bugs. Frequently you refresh a pivot and all parameters get lost. Now this will happen automatically. Bravo MS!
    • If one could ever put all the crap of the world in one box, that would be Microsoft.
    • The most corrupt and vile CEO any company ever had ! The only company that makes an insane amount of money decade after decade but doesn't deliver on even barely passable quality to customers, screws over partners and treats employees like trash
    • PSA: Some other versions of Windows are losing support on October 14 too by Usama Jawad All of us here at Neowin likely know pretty well by now that Windows 10 is reaching end of support on October 14, 2025. You can extend support through paid and "free" means, but if you don't, you won't get any more security or feature updates following the aforementioned date. We also highlighted that the Long-Term Servicing Channel (LTSC) version Windows 10 22H2 is also reaching end of support on that date. Now, Microsoft has reminded customers that yet another variant of Windows is reaching end of life on October 14, 2025 too. On the Windows Release Health dashboard, Microsoft has published a reminder that the Enterprise, Education, and IoT SKUs of Windows 11, version 22H2 will hit end of support on October 14 as well. It is important to keep in mind that the Home and Pro variants of Windows 11, version 22H2 already reached end of life on October 8, 2024, and the extra year of lease on life will end for other SKUs within a few months too. Windows 11, version 22H2 for IoT, Enterprise, and Education was released on September 20, 2022, which means that they would have received just over three years of support by the time they "die", compared to the regular two years for Home and Pro. Customers on any of these versions should consider upgrading to version 23H2 or 24H2 as soon as possible, which have end of support dates of November 10, 2026 and October 12, 2027, respectively. Staying on a supported version of Windows is crucial as that allows you to receive regular security updates on your machine. If you're an IT admin, you should immediately begin planning a migration to a supported version of the operating system, and if you're an employee or someone using these versions of Windows in some other scenario, go to Settings > System > About and check out Windows Specifications > Version.
  • Recent Achievements

    • Dedicated
      Profit earned a badge
      Dedicated
    • One Month Later
      hhgygy earned a badge
      One Month Later
    • Week One Done
      hhgygy earned a badge
      Week One Done
    • One Year In
      NIKI77 earned a badge
      One Year In
    • Week One Done
      artistro08 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      632
    2. 2
      ATLien_0
      237
    3. 3
      Xenon
      167
    4. 4
      neufuse
      146
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!