bjrnt
bjrnt

Reputation: 2822

AS3: Removing EventListeners without knowing amount or names

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.

Upvotes: 2

Views: 5658

Answers (3)

André Gil
André Gil

Reputation: 624

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);

;)

Upvotes: 1

Baelnorn
Baelnorn

Reputation: 940

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.

Upvotes: 1

jonathanasdf
jonathanasdf

Reputation: 2884

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.

Upvotes: 3

Related Questions