I'm working on a simple class that loads configuration information from a file. I would like to load the file and create an event in the document class to proceed with parsing the information once the data is loaded. I've done this in the past by creating a function in the child class and "overriding" the functionality by creating a reference to a new method in the parent class. For example:
//main.as - document class
package{
import flash.display.Sprite;
import loadingClass;
public class main extends Sprite{
var loadFile:loadingClass;
public function main():void{
loadFile = new loadingClass("somefile.json");
loadFile.onComplete = init;
}
public function init():void {
//the file is ready
}
}
}
//loadingClass.as
package {
import flash.net.*;
import flash.events.*;
public class loadingClass {
private var fileLoader:URLLoader;
//Define a function to be overwritten by the parent
public var onComplete:Function;
public function loadingClass(path:String):void{
if (path == null){return;}
var filePathRequest:URLRequest = new URLRequest(path);
fileLoader = new URLLoader (filePathRequest);
setListeners(fileLoader);
}
private function setListeners(dispatcher:IEventDispatcher):void{
dispatcher.addEventListener(Event.COMPLETE, createObj);
}
public function createObj(evt:Event):void{
//Call the onComplete function which should have been
//overwritten with a parent function
onComplete();
}
}
}
I suspect this isn't the "proper" way to do this - is there a generally preferred method, like using an IEventDispatcher? Ideally, I'd like the implementation to be something like:
//main.as
loadFile = new loadingClass("somefile.txt");
loadFile.addEventListener("onComplete", init);
