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.

Actionscript:
  1. /*
  2. * Class written by Xavier MARTIN (xxlm or zeflasher)
  3. * http://dev.webbymx.net
  4. * http://www.webbymx.net
  5. * If you are using this class, I will be glad to receive a postcard of your place :)
  6. * To do so please visit http://dev.webbymx.net and go in the about page to get my details...
  7. */
  8.  
  9. class net.webbymx.events.XClassEventListener {
  10.    
  11. /* *****************************************************************************
  12. * PRIVATE STATIC VARIABLES
  13. ***************************************************************************** */
  14.     private static var $instance : XClassEventListener = undefined;
  15.     private static var _oListeners : Object;
  16.    
  17.  
  18. /* *****************************************************************************
  19. * CONSTRUCTOR
  20. ***************************************************************************** */
  21.     private function XClassEventListener() {}
  22.  
  23. /* *****************************************************************************
  24. * PUBLIC STATIC FUNCTIONS
  25. ***************************************************************************** */
  26. /**
  27. * Initialize the singleton and delegate function
  28. * @param    obj
  29. */
  30.     public static function initialize( obj : Object ) : Void {
  31.         if ( $instance == undefined ) $instance = new XClassEventListener;
  32.         obj.addEventListener = $instance.addEventListener;
  33.         obj.removeEventListener = $instance.removeEventListener;
  34.     }
  35.  
  36. /* *****************************************************************************
  37. * PRIVATE STATIC FUNCTIONS
  38. ***************************************************************************** */
  39. /**
  40. * dispatch the event to a c
  41. * @param    eventObj
  42. */
  43.     public static function dispatchEvent( eventObj : Object ) : Void {
  44.     //  check if the object has the good properties
  45.         if ( eventObj.target == undefined || eventObj.clazz == undefined || typeof( eventObj.clazz ) != "string" ||
  46.             eventObj.type == undefined || typeof( eventObj.type ) != "string" ) return;
  47.  
  48.     //  check is the class end event is registered
  49.         if ( _oListeners[ eventObj.clazz ] == undefined || _oListeners[ eventObj.clazz ][ eventObj.type ] == undefined ) return;
  50.  
  51.     //  list object and call the function
  52.         for ( var i : String in _oListeners[ eventObj.clazz ][ eventObj.type ] ) {
  53.             var o : Object = _oListeners[ eventObj.clazz ][ eventObj.type ][ i ].o;
  54.             var oType : String = typeof( o );
  55.             var f : String = _oListeners[ eventObj.clazz ][ eventObj.type ][ i ].f;
  56.             if ( oType == "object" || oType == "movieclip" ) {
  57.                 if ( o.handleEvent != undefined && f == undefined ) {
  58.                     o.handleEvent( eventObj );
  59.                 } else {
  60.                     if ( f == undefined ) f = eventObj.type;
  61.                     o[ f ]( eventObj );
  62.                 }
  63.             }
  64.         }
  65.     }
  66.    
  67. //  internal function to locate listeners:
  68.     private static function $indexOfListener( listeners : Array, obj : Object, func : String) : Number {
  69.         var l:Number = listeners.length;
  70.         var i:Number = -1;
  71.         while ( ++i <l ) {
  72.             var tmpObj:Object = listeners[ i ];
  73.             if ( tmpObj.o == obj && func == undefined ) return i;
  74.             else if ( tmpObj.o == obj && tmpObj.f == func ) return i;
  75.         }
  76.         return -1;
  77.     }
  78.    
  79.  
  80. /* *****************************************************************************
  81. * PRIVATE FUNCTIONS
  82. ***************************************************************************** */
  83. /**
  84. * Add a listener for an event, for a class
  85. * @param    p_event: String
  86. * @param    p_obj: Function (Class.prototype)
  87. * @param    p_function: String
  88. */  // OK
  89.     private function addEventListener( event : String, clazz : String, func : String) : Void {
  90.         if ( _oListeners == undefined ) { _oListeners = {}; }
  91.        
  92.     //  creating a new object for each class holding the events
  93.         if ( _oListeners[ clazz ] == undefined ) _oListeners[ clazz ] = {};
  94.        
  95.     //  creating a array (same name as the function) holding the obj (ref, callbackfunction)
  96.         if ( _oListeners[ clazz ][ event ] == undefined ) _oListeners[ clazz ][ event ] = [];
  97.  
  98.     //  check if the object is already in the array, if not pushing it
  99.         if ( $indexOfListener( _oListeners[ clazz ][ event ], this, func ) == -1 ) _oListeners[ clazz ][ event ].push( { o : this, f : func } );
  100.     }
  101.  
  102.    
  103. /**
  104. * Remove the listener for an event, for a class
  105. * @param    p_event: String
  106. * @param    p_obj: Function (Class.prototype)
  107. * @param    p_function: String (optional)
  108. */
  109.     private function removeEventListener( event : String, clazz : String ) : Void {
  110.         var func:String = arguments[ 2 ];
  111.        
  112.     //  go out of the function if either the class is not registered nor the event for the class
  113.         if ( _oListeners == undefined || _oListeners[ clazz ] == undefined || _oListeners[ clazz ][ event ] == undefined ) return;
  114.  
  115.     /*  find the index for the object. If the function is pass as the parameter we will delete just the instance holding this callback
  116.         function for this class, else we will delete all the instance */   
  117.         var index:Number;
  118.         if ( arguments[ 2 ] != undefined ) {
  119.             index = $indexOfListener( _oListeners[ clazz ][ event ], this, func );
  120.             if ( index != -1 ) _oListeners[ clazz ][ event ].splice( index, 1 );
  121.         }
  122.         else {
  123.             while ( ( index = $indexOfListener( _oListeners[ clazz ][ event ], this ) ) != -1 ) {
  124.                 _oListeners[ clazz ][ event ].splice( index, 1 );
  125.             }
  126.         }
  127.  
  128.         if ( index != -1 ) {
  129.             if ( _oListeners[ clazz ][ event ].length == 0 ) delete _oListeners[ clazz ][ event ];
  130.             if ( _oListeners[ clazz ].length == 0 ) delete _oListeners[ clazz ];
  131.         }
  132.     }
  133.  
  134.    
  135. /**
  136. * Remove all the listener listening to an event of a class
  137. * @param    event
  138. * @param    clazz
  139. */
  140.     private function removeAllEventListener( event : String, clazz : String ) : Void {
  141.     //  go out of the function if either the class is not registered nor the event for the class
  142.         if ( _oListeners == undefined || _oListeners[ clazz ] == undefined || _oListeners[ clazz ][ event ] == undefined ) return;
  143.         delete _oListeners[ clazz ][ event ];
  144.         if ( _oListeners[ clazz ].length == 0 ) delete _oListeners[ clazz ];
  145.         if ( _oListeners.length == 0 ) delete _oListeners;
  146.     }
  147.    
  148.    
  149. /**
  150. * Remove all the listener listening to a class
  151. * @param    clazz
  152. */
  153.     private function removeClassListener( clazz : String ) : Void {
  154.     //  go out of the function if either the class is not registered nor the event for the class
  155.         if ( _oListeners == undefined || _oListeners[ clazz ] == undefined ) return;
  156.         delete _oListeners[ clazz ];
  157.     }
  158. }

XClassEventListener Class Download the class Unknown

Here is a quick example showing you how to use it.

First a class you're doing:

Actionscript:
  1. import net.webbymx.events.XClassEventListener;
  2. [Event "test"]
  3. classnet.webbymx.test.DClass{
  4.     functionDClass(){
  5.         trace("DCLassinstancecreated");
  6.     };
  7.     public function Dpatch () {
  8.         var o : Object = new Object ();
  9.         o.target = this;
  10.         o.type = "test";
  11.         o.clazz = "net.webbymx.test.DClass";
  12.         XClassEventListener.dispatchEvent (o);
  13.     }
  14. }

Then in your fla:

Actionscript:
  1. import net.webbymx.events.XClassEventListener;
  2. import net.webbymx.test.DClass;
  3.  
  4. XClassEventListener.initialize (this);
  5. this.addEventListener ("test", "net.webbymx.test.DClass", "atest");
  6.  
  7. var d1 : DClass = new DClass ();
  8. var d2 : DClass = new DClass ();
  9. var d3 : DClass = new DClass ();
  10. var d4 : DClass = new DClass ();
  11.  
  12. // test dispatching
  13. d1.Dpatch ();
  14. function atest (evtObj) {
  15.     trace ("callback done");
  16.     trace (evtObj.clazz);
  17.     this.removeEventListener ("test", "net.webbymx.test.DClass");
  18. }

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

Actionscript:
  1. XClassListener.initialize(this);
  2. this.addEventListener("throw", "net.webbymx.game.Player", "wait");

and this for the Opponent Team

Actionscript:
  1. XClassListener.initialize(this);
  2. this.addEventListener("throw", "net.webbymx.game.Player", "seek");

I hope you'll like it...

Related posts:

  1. AS2 – Vector Class
  2. MDM Zinc – Class FileSystemManager
  3. MDM Zinc – Class DateManager for ftp and filesystem

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

Tags: , ,

blog comments powered by Disqus