0

Can't seem to get the value of myXML outside the function, despite being declared outside. What am I missing here? The data loads and traces correctly inside the function.

var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("flightPlannerBoard.xml"));

var myXML:XML;

// Check XML data fully loaded
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void {

myXML = new XML(e.target.data);
//trace(myXML);
}
trace(myXML);
2
  • The last statement (trace(myXML);) is executed before the XML is loaded. To confirm this, create another function that traces XML content and call that function from processXML function after you create the XML. Commented Feb 27, 2012 at 9:05
  • Yep, that worked! ' function processXML(e:Event):void { myXML = new XML(e.target.data); myXMLtrace(); } function myXMLtrace(){ trace(myXML.*); }' Commented Feb 27, 2012 at 9:17

3 Answers 3

1

Because ActionScript is asyncronous as others have said, you cannot control the flow of execution by code placement. What you must do is control execution through the events, and so whatever actions you want to perform with the loaded XML should be in the processXML function or in another function that is called from processXML:

var myXML:XML;

function processXML(e:Event):void {
    myXML = new XML(e.target.data);
    trace(myXML);  //this trace will work
    doNextAction();
}

function doNextAction():void {
    trace(myXML);  //this trace will also work
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help, that's good to know for other stuff as I learn AS3.
0

You should declare your XML variable outside your function in order to be able to use it in another function

private var myXML:XML;

6 Comments

Isn't that what I did? (minus the Private)
I suppose the first four lines of codes are declared in the public function of your class, above that you should declare variables that you wan't to use in multiple functions
Tried but no change. Created a new FLA file and pasted in just this code but no change.
This is not relevant for timeline code, which I assume this is.
Correct. Haven't got that far with AS3 yet.
|
0

Actionscript is an asynchronous language, meaning the trace "outside" the callback will be called before the file has loaded. The execution order in your case is:

  1. create instance of URLLoader
  2. start loading file
  3. add event listener to listen to the complete event
  4. trace out myXML
  5. (or at some point later) finish loading xml file

2 Comments

processXML should only fire once the XML file is loaded - which is why it works inside. So, in this case, how can I get the value out of the function?
The setting of the value should be fine as it is, but it will only be available after the XML file has loaded, not when the execution reaches your trace method. You need to ensure that you do not try to use the myXML variable until the processXML-function is called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.