SHARE
TWEET

Eventsystem for AS2

ulfben Jan 15th, 2012 148 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private var _listeners:Object = {};
  2. public function Broadcaster(){
  3. }              
  4. public function addEventListener(eventName:String, listenerObj:Object, methodName:String):Void{
  5.         if(_listeners[eventName] == undefined){
  6.                 _listeners[eventName] = new Array();
  7.         }
  8.         _listeners[eventName].push({listenerObj:listenerObj, methodName:methodName});
  9. }      
  10. public function broadcastEvent(eventName:String, data:Object):Void{
  11.         var eventQueue:Array = new Array();
  12.         for(var i:Number = (listeners.length - 1); i >= 0; i--){
  13.                 var method:String = eventName;
  14.                 if(_listeners[eventName][i].methodName != undefined){
  15.                         method = _listeners[eventName][i].methodName;
  16.                 }
  17.                 eventQueue.push({listenerObj:_listeners[eventName][i].listenerObj, method:method})
  18.         }
  19.         for(var i:Number = (listeners.length - 1); i >= 0; i--){
  20.                 eventQueue[ii].listenerObj[eventQueue[ii].method](data);
  21.         }
  22. }      
  23. public function removeEventListener(eventName:String, listenerObj:Object, methodName:String):Void{
  24.         if(_listeners[eventName] == null){ return; }
  25.         var listeners:Array = _listeners[eventName];
  26.         for(var i:Number = (listeners.length - 1); i >= 0; i--){
  27.                 if(listeners[i].listenerObj == listenerObj && listeners[i].methodName == methodName){
  28.                         _listeners[eventName].splice(i, 1); //splice the member, not the local copy
  29.                         return;
  30.                 }
  31.         }      
  32. }
RAW Paste Data
Top