I’m trying to get a value from a function which is a URLLoader COMPLETE event but even with declaring variables outside of the function will not allow me to get the value out. I’m stuck.
It seems that the Asynchronous nature of Flash makes it impossible to get a value out: e.g. this works:
// Initialise a URLLoader to get XML data from XML file
var myFPBLoader:URLLoader = new URLLoader();
myFPBLoader.load(new URLRequest("flightPlannerBoard.xml"));
// Check XML data fully loaded
myFPBLoader.addEventListener(Event.COMPLETE, processFPBxml);
var myXML:XML = new XML();// Declared **outside** function
function processFPBxml(e:Event):void {
this.myXML = XML(e.target.data);
trace("yep" + myXML); // This works
myTraceXML();
}
function myTraceXML(){
trace("more tests:" + myXML); // **This trace works**
}
trace("more tests:" + myXML); // This **doesn't** work
How do you get a value you can use whenever, wherever you want?
Here’s what is happening in your code:
processFPBxml(),myTraceXML()and variablesmyFBPLoaderandmyXMLare declared.myFPBLoader.load. The code does not stop at this point. The loading is asynchronous call and the rest of the code is being executed immediately.trace("more tests:" + myXML); // This **doesn't** workEvent.COMPLETEis dispatched.processFPBxml(). At this point the myXML is assigned to the downloaded data.myTraceXML()is called.Now if you want to continue code execution after the XML is loaded, you have to divide your code into separate functions that make specific tasks and call those functions after you get the data from XML. In this case, you could rename
myTraceXML()toinit()or anything that makes sense to you and write the rest of the program (not necessary in this particular function. Use it as the starting point).