AS3: removing EventListeners without knowing the number or names

First, a short summary of how my site works: When the link is clicked, it checks that something is already displayed on the left or right side of the screen (the website looks like a book, so I have a left page where I want to display information and the right page). If there is already something showing that it hides it and displays a new object, along with it, it includes all the buttons inside that object (I have separate functions to customize each object).

An example of such an EventListener would be:

pathTo.Button1.addEventListener(MouseEvent.CLICK, function():void {showText(side, object)});

      

What I am trying to do is remove all previously installed EventListeners without having to create separate functions to remove references within each object.

Shorter version: How to remove all EventListeners for all objects inside another object? The only variable I want to store is the object containing everything. However, objects don't always have EventListeners.

+2


a source to share


3 answers


Yes it is possible and the solution is here: http://blog.andregil.net/2010/01/how-to-remove-event-listeners-with-parameters-closure/

You can use this snippet for a listener function to remove it from yourself:



event.currentTarget.removeEventListener(event.type, arguments.callee);

      

;)

+1


a source


you can check for existing event listeners using

hasEventListener(Event.TYPE)

      

and you can remove event listeners using



removeEventListener(Event.TYPE, listenerFunc)

      

Note that you must have a reference to listenerFunc in order to remove the event listener.

+3


a source


In the Flex framework, you can run some monkey patch to enable this functionality.

Doug McCune describes how to do this in this blog post .

Edit: Confuse your question a bit. As described, this only works for Flex components. Not sure if you can use the same technique for generic Flash components. Unfortunately.

+1


a source







All Articles