The event framework or EventListeners in ActionScript 3 is one of the best features that AS3 has, here a some tips and tricks that I have found very usefully.
Useing WeakReference:
In AS3 objects gets garbage collected if there are no references to them. If you remove your object where an event listener is added but if the object is still being referenced somewhere else it will not be garbage collected given you the possibility of having memory leakage (strong memory references). If you set your event listener with weak memory references the event listener gets garbage collected when the object it’s added to gets removed.
Priority:
If you didn’t know event listeners don’t work simultaneously sometimes they conflict with each other to fix this you may want to make sure that certain event listener have a higher priority then others.
One Listener Function, Multiple Events:
Sometimes you may want to save code or just write nicer and cleaner code a best practice for this is using one listener function for multiple events.
if(event.type == "mouseDown"){
trace("Down");
}else{
trace("Up");
}
}
movieClip.addEventListener(MouseEvent.MOUSE_DOWN, handler);
movieClip.addEventListener(MouseEvent.MOUSE_UP, handler);
One Event Listener, Multiple Objects:
So this is one of my favorite things in AS3, not only does it save time but its very usefully when you have large navigation menus. What you want to do is have a container movie clip which holds all the your other movie clip or you navigation, you will give that movie clip the event listener and use the event.target.name to identity which movie clips are being clicked on.
if(event.target.name == "movieClip1"){
trace("movieClip");
}
if(event.target.name == "movieClip2"){
trace("movieClip2");
}
if(event.target.name == "movieClip3"){
trace("movieClip3");
}
}
container.addEventListener(MouseEvent.CLICK, clickHandler);
Cleanup Up Multiple Event Listeners:
Sometimes times you need to remove multiple event listeners at once, this function overrides the standard addEventListener function, storing all added event listeners including parameters in an array. Once this is done you would call target.clearEvents() to remove all event listeners on that target.
function addEventListener (type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void { super.addEventListener (type, listener, useCapture, priority, useWeakReference);
myArrayListeners.push({type:type, listener:listener, useCapture:useCapture});
}
function clearEvents():void {
for (var i:Number=0; i < myArrayListeners.length; i++) {
if (this.hasEventListener(myArrayListeners[i].type)) {
this.removeEventListener(myArrayListeners[i].type,
myArrayListeners[i].listener);
}
}
myArrayListeners=null;
Using Key Codes:
On your keyboard every key has a certain number; a key code this is very useful for games and interactive kiosk’s You can fins a list of key codes here
if(event.keyCode == 38){
trace("You just pressed the arrow up key");
}
if(event.keyCode == 40){
trace("You just pressed the arrow down key");
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

















116 Bill Bartmann https://wso2.org/wiki/display/~farseechamp
Sep 03rd, 2009 at 06:09 am
This site rocks!
123 Bill Bartmann http://www.javaworld.com/community/user/26375
Sep 06th, 2009 at 05:09 pm
Hey good stuff…keep up the good work! :)
272 Rob Layton http://www.roblayton.net
Nov 15th, 2009 at 07:11 pm
Very useful tips, especially the one about one event listener, multiple objects. Thank you.
393 MichaelJW http://gamedev.michaeljameswilliams.com/
Dec 06th, 2009 at 02:12 pm
Great stuff! Thanks :)
394 seleneyue
Dec 07th, 2009 at 08:12 am
I tried your method of cleaning up event listeners, but it didn’t work.
Is there any specific implementation required?
395 Almog http://www.almogdesign.net
Dec 09th, 2009 at 09:12 pm
Hi Seleneue, did you make sure to push all your event listeners to an array, also did you run the clearEvents() function. If you can’t get it to work send me the files I will try to take a look.
401 tom335
Dec 23rd, 2009 at 02:12 am
congrats! very helpful stuff… thnx
418 Uyanga
Jan 12th, 2010 at 08:01 am
absolutely kewl!!!! super helpful. thank you
420 JH
Jan 15th, 2010 at 04:01 pm
Finally, someone gives the shortcut to us noobs for one event listener, multiple objects. AND I actually got it to work! Thanks…
427 Enrique http://www.enriquepiatti.com
Jan 21st, 2010 at 11:01 pm
There are two errors:
First:
movieClip.addEventListener(MouseEvent.CLICK, true, clickHandler);
This call is wrong, should be:
movieClip.addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);
If you want to use capture and weak reference.
And here:
myArrayListeners.push({type:type, listener:listener});
You should add useCapture to the array, because it’s neccesary to delete correctly the listener. It should be:
myArrayListeners.push({type:type, listener:listener, useCapture:useCapture});
and should be used in this way:
for (var i:Number=0; i < myArrayListeners.length; i++)
{
this.removeEventListener(myArrayListeners[i].type,
myArrayListeners[i].listener, myArrayListeners[i].useCapture);
}
note We don’t need to check hasEventListener because “if there is no matching listener registered with the EventDispatcher object, a call to this method has no effect.”
greets!