How to remove all event listeners from a display object?

后端 未结 5 893
天涯浪人
天涯浪人 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 15:47

    jeceuyper is right ...

    a side not though: DisplayObject extends EventDispatcher, which already does implement IEventDispatcher ... so to be more precise: you need to override addEventListener and removeEventListener to keep track of the listeners ...

    a few technical details: i suggest you use Dictionary to store the handler functions ... a bit slower for insertion, but much faster for removal ... also, Dictionary supports weak references, which is quite important in the case of event handling ... also keep in mind, that useCapture allows to add the same handler twice ...

    good luck then ... ;)

    0 讨论(0)
  • 2021-02-08 15:47

    Glenn is right, there is no such thing as a removeAllListener or listAllListener method. Nevertheless, you could make your custum diplayObject implement the IEventDispatcher interface and keep track of all the listeners added or removed from your object.

    0 讨论(0)
  • 2021-02-08 15:47
    function a(){
        mc.addEventListener(Event.ENTER_FRAME,function(){
                           ...
                           }
    }
    
    function b(){
        mc.removeEventListener(Event.ENTER_FRAME,function(){});
    }
    

    works...

    0 讨论(0)
  • 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){}
    }
    
    0 讨论(0)
  • This is sort of a hack, but in some (perhaps most cases), you can easily set the display object to null and re-initialize it and then re-configure it with zero visual disruption.

    This has the added bonus of removing all event listeners.

    Unless you are doing this in an app that already has hundreds of listeners and objects then it should work perfectly fine so long as you can tolerate reconfiguring your display object.

    Obviously, you shouldn't do this on anything that is doing something crazy in the constructor like loading data.

    0 讨论(0)
提交回复
热议问题