I wrote a subclass of EventCurb for this purpose, see here here or paste below.
package { import flash.events.EventDispatcher; import flash.utils.Dictionary; /** * ... * @author Thomas James Thorstensson * @version 1.0.1 */ public class EventCurb extends EventDispatcher { private static var instance:EventCurb= new EventCurb(); private var objDict:Dictionary = new Dictionary(true); private var _listener:Function; private var objArr:Array; private var obj:Object; public function EventCurb() { if( instance ) throw new Error( "Singleton and can only be accessed through Singleton.getInstance()" ); } public static function getInstance():EventCurb { return instance; } override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void { super.addEventListener(type, listener, useCapture, priority, useWeakReference); } override public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void { super.removeEventListener(type, listener, useCapture); } public function addListener(o:EventDispatcher, type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void { // the object as key for an array of its event types if (objDict[o] == null) objArr = objDict[o] = []; for (var i:int = 0; i < objArr.length; i++) { if ( objArr[i].type == type) trace ("_______object already has this listener not adding!") return } obj = { type:type, listener:listener } objArr.push(obj); o.addEventListener(type, listener, useCapture, priority, useWeakReference); } public function removeListener(o:EventDispatcher, type:String, listener:Function, useCapture:Boolean = false):void { // if the object has listeners (ie exists in dictionary) if (objDict[o] as Array !== null) { var tmpArr:Array = []; tmpArr = objDict[o] as Array; for (var i:int = 0; i < tmpArr.length; i++) { if (tmpArr[i].type == type) objArr.splice(i); } o.removeEventListener(type, listener, useCapture); if (tmpArr.length == 0) { delete objDict[o] } }else { trace("_______object has no listeners"); } } /** * If object has listeners, returns an Array which can be accessed * as array[index].type,array[index].listeners * @param o * @return Array */ public function getListeners(o:EventDispatcher):Array{ if (objDict[o] as Array !== null) { var tmpArr:Array = []; tmpArr = objDict[o] as Array; // forget trying to trace out the function name we use the function literal... for (var i:int = 0; i < tmpArr.length; i++) { trace("_______object " + o + " has event types: " + tmpArr[i].type +" with listener: " + tmpArr[i].listener); } return tmpArr }else { trace("_______object has no listeners"); return null } } public function removeAllListeners(o:EventDispatcher, cap:Boolean = false):void { if (objDict[o] as Array !== null) { var tmpArr:Array = []; tmpArr = objDict[o] as Array; for (var i:int = 0; i < tmpArr.length; i++) { o.removeEventListener(tmpArr[i].type, tmpArr[i].listener, cap); } for (var p:int = 0; p < tmpArr.length; p++) { objArr.splice(p); } if (tmpArr.length == 0) { delete objDict[o] } }else { trace("_______object has no listeners"); } } } }
Thomas Thorstensson
source share