im new to AS3. Looking to save data that i’ve loaded from an xml file into a variable. then call that variable later on.
heres my code:
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;
import flash.text.TextField;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
public class myClass extends MovieClip {
public static var objectOneTotal:int = 1;
public static var objectOneCurrent:int = 1;
public function myClass() {
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("beers.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(event:Event):void {
myXML = new XML(event.target.data);
myClass.objectOneCurrent = int(myXML.current);
myClass.objectOneTotal = int(myXML.total);
trace(myClass.objectOneCurrent);
trace(myClass.objectOneTotal);
}
trace(myClass.objectOneCurrent);
trace(myClass.objectOneTotal);
var objectOneStart:int = objectOne.x;
objectOne.x = (objectOneCurrent / objectOneTotal) * (finish.x - objectOneStart);
}
}
}
What its doing: when i run it, looking at the traces, its tracing the objects initial values of “1” before its tracing the values which are loaded from the xml. So i believe it is loading the data, but the program is only running the processXML function after the rest of the code. or something.
Im not really sure. but i need the last two traces to read the xml data.
Thanks
Because AS3 is asyncronous, it will execute the entirety of the
MyClassmethod before the XML is loaded. When the XML does load, the code defined inprocessXMLis executed to handle it.The solution to this is to make
processXMLhandle setobject1.x: