Click Listener

Actionscript:
  1. var e:String = "addEventListener";
  2.  
  3. stage[e]("click", function():void{
  4.     trace("clicked", arguments[0]);
  5. });

Strange code to do a click listener. arguments[0] is the event object. (Check out the "warning" page for more info about this).

Actionscript:
  1. stage.addEventListener(MouseEvent.CLICK, onStageClick);
  2. function onStageClick(evt:MouseEvent):void {
  3.     trace("clicked", evt);
  4. }

Normal way.

This entry was posted in Events. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted November 6, 2008 at 7:30 pm | Permalink

    Not a very good way as I see it. You loose your reference to the method that listens for the event, causing a possible memory leak. I could see using this way to call dynamic methods based on information loaded at runtime, but I don’t see a benefit in using it to add a listener.

  2. Posted November 7, 2008 at 8:04 am | Permalink

    Yes. I agree with you… check out the “warning” page. I actually use this example as something that you shouldn’t do. That’s why I included the normal way at the end.

    Using anonymous functions is risky. One easy way to clean them up is with arguments.callee.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*