Click Listener

var e:String = "addEventListener";

stage[e]("click", function():void{
	trace("clicked", arguments[0]);
});

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

stage.addEventListener(MouseEvent.CLICK, onStageClick);
function onStageClick(evt:MouseEvent):void {
	trace("clicked", evt);
}

Normal way.

2 comments

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

Leave a Reply

Your email address will not be published. Required fields are marked *