Feed Rss



Jan 16 2010

Remove All Eventlisteners from an Actionscript 3.0 Component, A Workaround

category: Flex author: Maarte


What i’ve heard and read, it’s just NOT DONE.
Unless you play the cards a little.

mAARTE.be has 1 linkbutton for downloading my project files, it lets you download the files based on which project you are viewing.

So, when i generate content for a project, i add a listener to the linkbutton like so :

projLink.addEventListener(MouseEvent.CLICK, function clicked(e:Event):void{
launchProfile('assets/Sabrina.zip');
});

If the viewer chooses to view another project, the listener is re-generated like this :

projLink.addEventListener(MouseEvent.CLICK, function clicked(e:Event):void{
launchProfile('assets/Thesis.zip');
});

What happends is, that my linkbutton now has 2 MouseEvent.CLICK event listeners, which might cause it to launch the wrong file. This was very noticeable when maarte.be was clicked.

So in order to add a new listener, i needed to remove the others.
There isn’t a method called removeAllEventlisteners(), so i had to delete them one by one, which isn’t possible cause the functions are anonymous.

My second thought was to re-create the linkbutton object, but this also not done, you can’t provide a showEffect via AS.

There is a way to delete the previous event handler, by adding this line of code before you add the new Listener :

projLink.removeEventListener(MouseEvent.CLICK, arguments.callee);

arguments.callee is an ancient built-in flash function that calls out to the given arguments of the function you’re in, in this case : the anonymous function.

All my linkbutton.addEventListener’s look like this now :

projLink.addEventListener(MouseEvent.CLICK, function clicked(e:Event):void{
projLink.removeEventListener(MouseEvent.CLICK, arguments.callee);
launchProfile('assets/Sabrina.zip');
});

Succes !

This is not a way to delete all listeners at once, but it’s a great start to delete the previous one :-)

mAARTE.be should be fully bugfree by now.
Okay, the linkbutton hovers over the textarea, hear me out :
I always hated it that, in order to get the source code, you had to scroll down to the end of the article and look for the “you can find it here”-button. In this case you can download the source right away without scrolling.
I just expanded your mouse’s life by 0.4 years !

Big java-solliciting-test coming up on monday.
This weekend, we Java !

We can’t all the credit for it, you can find more details here

Leave a Reply