How to handle loading data in objects in ActionScript 3
I'm coding a website in pure AS3 using FlashDevelop and have an obj开发者_开发知识库ect that loads an XML file on init. Is there a good way to make my main function wait until it has finished loading? I know about onComplete events and how to use them internally in the object but I'm not sure how to proceed.
Thanks in advance!
package
{
import flash.display.Sprite;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
public class HandleXMLData extends Sprite
{
private var xmlData:XML;
private var XMLFileURL:String = "myXML.xml";
public function HandleXMLData()
{
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
xmlLoader.addEventListener(Event.COMPLETE, xmlDataHandler);
xmlLoader.load(new URLRequest(XMLFileURL));
}
private function xmlDataHandler(evt:Event):void
{
evt.target.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
evt.target.removeEventListener(Event.COMPLETE, xmlDataHandler);
xmlData = new XML(evt.target.data);
init();
}
private function errorHandler(evt:IOErrorEvent):void
{
throw(evt.text);
}
private function init():void
{
//Initialization Routine
}
}
}
精彩评论