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. 532  

    May 07th, 2010 at 12:05 pm

    Thanks for your code but in my application i have 4 actioscipt files,so where to call this function is not clearly understood,
    all actionscript files are related to each other.

  2. 564 http://na  

    May 21st, 2010 at 08:05 am

    Having an issue, hoping you have some insight:

    object.addEventListener(event, handler);
    object.addEventListener(event, handler2);

    I have a button [object]. I add two event handlers for the same event. The first handler modifies the state of the button, while the second handler makes an http call.

    Only one event handler is being called (and it appears to be a random race condition).

    Solution?

    Cheers

  3. 575 http://www.almogdesign.net  

    May 22nd, 2010 at 10:05 am

    what type of event are you calling? If your calling an click event you can only have one. You might want to try the following

    addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
    addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);

    For changing the state of the button and a click event for the http call

    addEventListener(MouseEvent.CLICK, clickHandler);

  4. 649  

    Jun 04th, 2010 at 12:06 pm

    GREAT stuff !!

    However Using WeakReference:

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

  5. 692  

    Jun 16th, 2010 at 03:06 pm

    thnx…..i find it very useful and informative.

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.