Forums: Flash:

 

Custom events on classes

first
 

blacksanta_69 Custom events on classes

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);

 

lithium

you can extend EventDispatcher, which will give you all the event handling stuff for free, or you can instantiate an instance of eventdispatcher in a class and route your events through that. eg:


/** By extending EventDispatcher **/

package
{

import flash.events.*;

public class Test extends EventDispatcher
{
public function Test()
{
super(this);

// Now you're good to add, remove and dispatch events with this class
}
}

}

/** Without extending EventDispatcher **/

package
{

import flash.events.*;

public class Test
{
protected var dispatcher:EventDispatcher;

public function Test()
{
super();

dispatcher = new EventDispatcher ( this );
// Now you have to route the methods through your dispatcher instance.

}

public function addEventListener ( type : String, method : Function /* all the other ones here */ ) : void
{
dispatcher.addEventListener ( type, method );
}

public function removeEventListener ( type : String, method : Function ) : void
{
dispatcher.addEventListener ( type, method );
}

public function dispatchEvent ( evt:Event ):void
{
dispatcher.dispatchEvent ( evt );
}
}

}

 
first
 

Forums: Flash: Custom events on classes

 
New Post
 
You must be logged in to post