flash - How do I wait for an external .txt file to load in ActionScript 3? -
How do I wait for the external .txt file to load into action script 3? If I use the URL loader, then I have no guarantee that the file has been loaded because it transmits an event when it is completed. I'm calling the loader function from another class, so I can not just stop the next actions in the event listener. You can use the URLLoader to load a text file (or a XML file).
var loader: URLLoader; Loader = new URL loader (); Loader.addEventListener (event.complete, xml loaded); Var Request: URLRequest = New URLRequest ("file.xml"); Loader.load (request); Function xml loaded (event: event): zero {var myXML: XML = new XML (loader.data); } If you are calling it from another category, you can pass the caller as the context of EventListener and handle the event. From the Completed Class:
loader.addEventListener (event.complete, caller_class.xml loaded); Ideally, you can make your loader class reusable and extend it to Event Dispatcher so that you listen to your own class instead of listening to the URL loader. I am not posting code for this, but the answer is simple, but here are some links if you are interested and I hope it helps :)
Edit: Apparently you are looking for the way to the Dispatch Event: Some tips: 1) Increase your class to EventDispatcher < Pre> public class Expands YourClass EventDispatcher {
2) Use DispatchesEvent (New EVE T ( "event_name")); to post the event
3) Listen to this event from the second category. Example: loader_class.addEventListener ("event_name", callback); Optionally you can change DispatcherClass string ("event_name") EVENT_NAME continuous dispatcher class public contract EVENT_NAME: string = By defining "event_name", and by calling another class like this: loader_class.addEventListener (DispatcherClass.EVENT_NAME), callback);
I hope everything is now clear.
Comments
Post a Comment