Blog

AS2 – Listening the event of a CLASS

This class has been made to be able to listen to the events of a CLASS not to the on of an instance.

First the source code.

[as]
/*
* Class written by Xavier MARTIN (xxlm or zeflasher)
* http://dev.webbymx.net
* http://www.webbymx.net
* If you are using this class, I will be glad to receive a postcard of your place 🙂
* To do so please visit http://dev.webbymx.net and go in the about page to get my details…
*/

class net.webbymx.events.XClassEventListener {

/* *****************************************************************************
* PRIVATE STATIC VARIABLES
***************************************************************************** */
private static var $instance : XClassEventListener = undefined;
private static var _oListeners : Object;

/* *****************************************************************************
* CONSTRUCTOR
***************************************************************************** */
private function XClassEventListener() {}

/* *****************************************************************************
* PUBLIC STATIC FUNCTIONS
***************************************************************************** */
/**
* Initialize the singleton and delegate function
* @param obj
*/
public static function initialize( obj : Object ) : Void {
if ( $instance == undefined ) $instance = new XClassEventListener;
obj.addEventListener = $instance.addEventListener;
obj.removeEventListener = $instance.removeEventListener;
}

/* *****************************************************************************
* PRIVATE STATIC FUNCTIONS
***************************************************************************** */
/**
* dispatch the event to a c
* @param eventObj
*/
public static function dispatchEvent( eventObj : Object ) : Void {
// check if the object has the good properties
if ( eventObj.target == undefined || eventObj.clazz == undefined || typeof( eventObj.clazz ) != “string” ||
eventObj.type == undefined || typeof( eventObj.type ) != “string” ) return;

// check is the class end event is registered
if ( _oListeners[ eventObj.clazz ] == undefined || _oListeners[ eventObj.clazz ][ eventObj.type ] == undefined ) return;

// list object and call the function
for ( var i : String in _oListeners[ eventObj.clazz ][ eventObj.type ] ) {
var o : Object = _oListeners[ eventObj.clazz ][ eventObj.type ][ i ].o;
var oType : String = typeof( o );
var f : String = _oListeners[ eventObj.clazz ][ eventObj.type ][ i ].f;
if ( oType == “object” || oType == “movieclip” ) {
if ( o.handleEvent != undefined && f == undefined ) {
o.handleEvent( eventObj );
} else {
if ( f == undefined ) f = eventObj.type;
o[ f ]( eventObj );
}
}
}
}

// internal function to locate listeners:
private static function $indexOfListener( listeners : Array, obj : Object, func : String) : Number {
var l:Number = listeners.length;
var i:Number = -1;
while ( ++i < l ) { var tmpObj:Object = listeners[ i ]; if ( tmpObj.o == obj && func == undefined ) return i; else if ( tmpObj.o == obj && tmpObj.f == func ) return i; } return -1; } /* ***************************************************************************** * PRIVATE FUNCTIONS ***************************************************************************** */ /** * Add a listener for an event, for a class * @param p_event: String * @param p_obj: Function (Class.prototype) * @param p_function: String */ // OK private function addEventListener( event : String, clazz : String, func : String) : Void { if ( _oListeners == undefined ) { _oListeners = {}; } // creating a new object for each class holding the events if ( _oListeners[ clazz ] == undefined ) _oListeners[ clazz ] = {}; // creating a array (same name as the function) holding the obj (ref, callbackfunction) if ( _oListeners[ clazz ][ event ] == undefined ) _oListeners[ clazz ][ event ] = []; // check if the object is already in the array, if not pushing it if ( $indexOfListener( _oListeners[ clazz ][ event ], this, func ) == -1 ) _oListeners[ clazz ][ event ].push( { o : this, f : func } ); } /** * Remove the listener for an event, for a class * @param p_event: String * @param p_obj: Function (Class.prototype) * @param p_function: String (optional) */ private function removeEventListener( event : String, clazz : String ) : Void { var func:String = arguments[ 2 ]; // go out of the function if either the class is not registered nor the event for the class if ( _oListeners == undefined || _oListeners[ clazz ] == undefined || _oListeners[ clazz ][ event ] == undefined ) return; /* find the index for the object. If the function is pass as the parameter we will delete just the instance holding this callback function for this class, else we will delete all the instance */ var index:Number; if ( arguments[ 2 ] != undefined ) { index = $indexOfListener( _oListeners[ clazz ][ event ], this, func ); if ( index != -1 ) _oListeners[ clazz ][ event ].splice( index, 1 ); } else { while ( ( index = $indexOfListener( _oListeners[ clazz ][ event ], this ) ) != -1 ) { _oListeners[ clazz ][ event ].splice( index, 1 ); } } if ( index != -1 ) { if ( _oListeners[ clazz ][ event ].length == 0 ) delete _oListeners[ clazz ][ event ]; if ( _oListeners[ clazz ].length == 0 ) delete _oListeners[ clazz ]; } } /** * Remove all the listener listening to an event of a class * @param event * @param clazz */ private function removeAllEventListener( event : String, clazz : String ) : Void { // go out of the function if either the class is not registered nor the event for the class if ( _oListeners == undefined || _oListeners[ clazz ] == undefined || _oListeners[ clazz ][ event ] == undefined ) return; delete _oListeners[ clazz ][ event ]; if ( _oListeners[ clazz ].length == 0 ) delete _oListeners[ clazz ]; if ( _oListeners.length == 0 ) delete _oListeners; } /** * Remove all the listener listening to a class * @param clazz */ private function removeClassListener( clazz : String ) : Void { // go out of the function if either the class is not registered nor the event for the class if ( _oListeners == undefined || _oListeners[ clazz ] == undefined ) return; delete _oListeners[ clazz ]; } } [/as] [download]XClassEventListener Class;Download the class;http://dev.webbymx.net/wp-content/dld/register/flash/as2/classes/net/webbymx/events/XClassEventListener.as;/home/zeflasher/http/webbymx.net/dev/wp-content/dld/register/flash/as2/classes/net/webbymx/events/XClassEventListener.as;[/download] Here is a quick example showing you how to use it. First a class you're doing: [as] import net.webbymx.events.XClassEventListener; [Event "test"] classnet.webbymx.test.DClass{ functionDClass(){ trace("DCLassinstancecreated"); }; public function Dpatch () { var o : Object = new Object (); o.target = this; o.type = "test"; o.clazz = "net.webbymx.test.DClass"; XClassEventListener.dispatchEvent (o); } } [/as] Then in your fla: [as] import net.webbymx.events.XClassEventListener; import net.webbymx.test.DClass; XClassEventListener.initialize (this); this.addEventListener ("test", "net.webbymx.test.DClass", "atest"); var d1 : DClass = new DClass (); var d2 : DClass = new DClass (); var d3 : DClass = new DClass (); var d4 : DClass = new DClass (); // test dispatching d1.Dpatch (); function atest (evtObj) { trace ("callback done"); trace (evtObj.clazz); this.removeEventListener ("test", "net.webbymx.test.DClass"); } [/as] why did I create this class? It cames with a rugby game I was developing. I have players in two teams. The player are instance of the same class. When a player throw the ball I want the other teammate to act like "wait for ball" and the opponent like "seek for ball". So I'm doing this for the team player [as] XClassListener.initialize(this); this.addEventListener("throw", "net.webbymx.game.Player", "wait"); [/as] and this for the Opponent Team [as] XClassListener.initialize(this); this.addEventListener("throw", "net.webbymx.game.Player", "seek"); [/as] I hope you'll like it...