views:

122

answers:

3

Hello!

First shortly about how my site works: When a link is clicked it checks if something is already displayed in either the Left or Right side of the screen (the website looks like a book, so I have a left page I want to display information on and a right page). If there is already something showing it hides it and displays the new object, together with this it enables all the buttons within that object (I have separate functions to set up each object).

An example of such an EventListener would be:

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

What I'm trying to do is to remove all the previous set EventListeners without having to create separate functions for removing the links inside every object as well.

Shorter version: How do I remove all EventListeners on all objects inside another object? The only variable I want to store is the object containing everything. There are however not always EventListeners within the objects.

+2  A: 

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 the listenerFunc to remove the event listener.

jonathanasdf
Then I'm guessing it won't work the way I'm creating my functions? If I have to create separate functions for each call I might as well just create a "unload" function for each object?
DevEight
not necessarily. arguments.callee would get you the reference to the listenerFunc, although the use of it is not suggested.
jonathanasdf
+1  A: 

You could do some monkey-patching on the Flex framework to include that functionality.

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

Edit: Misread your question a bit. As described this only works for Flex components. Not sure if you could use the same method for general Flash components. Sorry.

Baelnorn
+1  A: 

Yes, it's 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 on the listener function, to make it remove the listener from itself:

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

;)

André Gil