I'm a noob with as3 and I'm trying to learn.
This is something that I cannot understand. I have a very simple XML:
<?xml version="1.0" encoding="utf-8"?>
<SLIDES>
<IMAGE>01.jpg</IMAGE>
<IMAGE>02.jpg</IMAGE>
<IMAGE>03.jpg</IMAGE>
<IMAGE>04.jpg</IMAGE>
<IMAGE>05.jpg</IMAGE>
</SLIDES>
That I'm parsing like this:
private function loadXML(filename:String):void {
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest(filename+".xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
myXML.ignoreWhite=true;
for (var i:int = 0; i < myXML.IMAGE.length(); i++) {
imagesURLs.push(myXML.IMAGE[i]);
trace(myXML.IMAGE[i]);
}
}
for each (var imageURL:String in imagesURLs) {
trace(imageURL);
}
}
The first trace returns:
01.jpg
02.jpg
03.jpg
04.jpg
05.jpg
Whi开发者_开发问答le the second is empty! Why? I'm really starting to lose my mind. I also tried:
myXML.IMAGE[i].toXMLString()
With no luck. Can you please help me and make me undestand why does this happens?
Thank you.
p.s.
The function gets called with the filename I need to parse and the array is declared like so:
public var imagesURLs:Array = [];
I'm using the latest version of FLEX with flashDevelop
Your second loop is executed before the 'processXML' function. At this time your Array is empty ! Your 'processXML' function will be called only when data will be available (asynchronous call) Also, don't forget to remove your event listener.
精彩评论