How to remove all event listeners from a display object?

后端 未结 5 896
天涯浪人
天涯浪人 2021-02-08 15:21

Is there a way to determine which event listeners are registered with a display object? I want to remove all event listeners from a display object so that I can assign new ones

5条回答
  •  长情又很酷
    2021-02-08 16:04

    back2dos has mentioned the approach you should use, what i did was extend the movieclip class and implemented all kinds of functions that i use on daily basis but are not part of the movieclip class. including the override for the addEventListener class

    protected var listeners : Dictionary    = new Dictionary();
    override public function addEventListener( type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = true) : void
    {
            var key : Object = {type:type,useCapture:useCapture};
            if( listeners[ key ] ) {
                    removeEventListener( type, listeners[ key ], useCapture );
                    listeners[ key ] = null;
            }
            listeners[ key ] = listener;
    
            super.addEventListener( type, listener, useCapture, priority, useWeakReference );
    }
    protected function removeListeners () : void
    {
            try
            {
                for (var key:Object in listeners) {
                        removeEventListener( key.type, listeners[ key ], key.useCapture );
                            listeners[ key ] = null;
                }
            }catch(e:Error){}
    }
    

提交回复
热议问题