Reputation: 4555
I'm learning as3 and I'm having difficulties understanding events.
I'm trying to load options inside an array that I will later need to access to load images.
Now..
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
myLoader.load(new URLRequest("slides.xml"));
myLoader.addEventListener(Event.COMPLETE, processXMLSlides);
...
}
private function processXMLSlides(e:Event):void {
removeEventListener(Event.COMPLETE, processXMLSlides);
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]);
}
//Start the main routine.
loadImages();
writeImage(imageCurrent);
}
loadImages() takes the array imagesURLs and loads them inside another array as URLRequests and then writeImage() writes the image to the stage.
Now the thing is this. If I move the loadImages() and writeImage() function here:
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
myLoader.load(new URLRequest("slides.xml"));
myLoader.addEventListener(Event.COMPLETE, processXMLSlides);
//Start the main routine.
loadImages();
writeImage(imageCurrent);
...
}
It doesn't work because loadImages gets called BEFORE the array gets populated from the xml. Now how can I tell as3 to wait for the processXMLSlides to finish its thing? Do I need another event? Is this stupid (there are better ways to do what I'm doing)?
Thanks
Upvotes: 0
Views: 134
Reputation: 17217
this is untested code, but something like this should work:
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
myLoader.addEventListener(Event.COMPLETE, processXMLSlides);
myLoader.load(new URLRequest("slides.xml"));
}
private function processXMLSlides(e:Event):void
{
e.currentTarget.removeEventListener(Event.COMPLETE, processXMLSlides);
myXML = new XML(e.target.data);
myXML.ignoreWhite = true;
for each (var element:XML in myXML..IMAGE)
{
var imageLoader:Loader = new Loader();
imageLoader.addEventListener(Event.COMPLETE, writeImage);
imageLoader.load(new URLRequest(element));
}
}
private function writeImage(e:Event):void
{
e.currentTarget.removeEventListener(Event.COMPLETE, writeImage);
addChild(evt.currentTarget);
}
you might want to stick with the var i:uint for loop, though, as you could use it to position the placement of each of your images.
have a read of the Loader class. there are many examples provided for class specific eventss you could use for your specific needs.
Upvotes: 1
Reputation: 8629
Your question doesn't make sense, your first example is doing exactly what you are asking for - waiting for the XML to be loaded.
Flash Events are very handy in handling asynchronous processes - like XML loading - how long would you wait otherwise before calling the loadImages();
and writeImage(imageCurrent);
? The answer is exactly as long as it takes for the XML to load. So keep it where it is, in the event handler method.
NOTE:
Add the listener to myLoader first and call the load() method only afterwards. When testing on a local environment (from a hard drive) it can happen that the "slides.xml" will load immediately and the COMPLETE event will be fired before the eventListener is attached.
Upvotes: 2