My Blog

ActionScript IconThe 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.

addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

movieClip.addEventListener(MouseEvent.CLICK, true, clickHandler);

 

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.

 

addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void

movieClip.addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true);

 

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.

 

function handler(event:MouseEvent):void{
    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.

 

function clickHandler(event:MouseEvent):void{
    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.

 

var myArrayListeners:Array=[];

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

 

function keyDownHandler(event:KeyboardEvent):void{
    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);
To Delicious, Stumbleupon, Digg, And More
 

Comments

There are 15 comments (+ Add Comment)
  1. 116 https://wso2.org/wiki/display/~farseechamp  

    Sep 03rd, 2009 at 06:09 am

    This site rocks!

  2. 123 http://www.javaworld.com/community/user/26375  

    Sep 06th, 2009 at 05:09 pm

    Hey good stuff…keep up the good work! :)

  3. 272 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.

  4. 393 http://gamedev.michaeljameswilliams.com/  

    Dec 06th, 2009 at 02:12 pm

    Great stuff! Thanks :)

  5. 394  

    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?

  6. 395 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.

  7. 401  

    Dec 23rd, 2009 at 02:12 am

    congrats! very helpful stuff… thnx

  8. 418  

    Jan 12th, 2010 at 08:01 am

    absolutely kewl!!!! super helpful. thank you

  9. 420  

    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…

  10. 427 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!

Post Comment

    • Please keep comments related to topic. And be nice, don't spam!
    • Basic HTML tags are allowed:
      <a href> <abbr> <acronym> <blockquote> <code> <em> <strike> <strong>
    • Note: un-related or spam comments will be deleted.
I just did an all nighter to get a prorject out the door, hate it when that happens.

Big’s Online E-commerce

Big’s Online e-commerce site powered by Magento.

Nokia Facebook App

I was tasked with the development of a interactive Facebook application and game for Nokia.

Me and Stuart Varrall going over concept
Me and Stuart Varrall presenting
Nokia Open Lab Milan 2008
Me and Stuart Varrall getting ready
Nokia phones
Power of the of the mobile