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
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();andwriteImage(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.