0

I'm coding a website in pure AS3 using FlashDevelop and have an object 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!

1 Answer 1

1
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
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

HandleXMLData needn't extend Sprite. +1
it does if it's the document class - which i think is what he means by "main" class
Just a note. I think you shouldn't throw on errorHandler. There's no way to catch the error: you cannot catch it with a try/catch since it's an asynchronous ErrorEvent; you are not re-dispatching it, so it cannot be caught with an event listener either.
i wrote it like this simply for debugging/development purposes. how would you handle the error for users at runtime? i mean, maybe a popup window with the error message, but what would you suggest?
Yes, I understand. For users, I'd probably go with some kind of alert or message box to let them know something went wrong. That is, if they need to know; many times, the user doesn't need to be bothered with such details, IMO. In this case however, it seems like if the loading didn't succeed, the app won't work, so it's reasonable to give them some kind of error message. On the other hand, I think excepctions are to be caught by code and act upon accordingly (when reasonable), not to be displayed to users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.