Listener Repetition Prevention

The same event type and listener combo will only be added once:
For the following example code, when the mouse clicks on the stage, clickHnd() is only called once. And since there is only one listener for MouseEvent.CLICK, only that listener is removed in clickHnd().

protected function setup():void
    stage.addEventListener(MouseEvent.CLICK, clickHnd, false, 1, true);
    stage.addEventListener(MouseEvent.CLICK, clickHnd2, false, 2, true);
    stage.addEventListener(MouseEvent.CLICK, clickHnd, false, 3, true);  // duplicate except for higher priority

protected function clickHnd2(e:MouseEvent):void

protected function clickHnd(e:MouseEvent):void
    e.currentTarget.removeEventListener(e.type, clickHnd);

I used e.currentTarget rather than to remove the listener because the listener is on the stage, not on the clicked object. This is not always needed, and often currentTarget will give the wrong results; so know what you are calling, and why, or test it!
See Adobe’s capture-bubble explanation for more details on target vs. currentTarget, and events in general.

So you can repeat adding events to an object, and it won’t add any processing time except for the check to see if it already exists.

Reference Type

Note that the above event listeners use weak reference. This means that if the object that they are attached to gets deleted, then these listeners get deleted by the garbage collector.  Above, they are attached to the ‘stage’ object; in use, though, often times they will be attached to some other foreign object. In order to make these listeners NOT get garbage-collected, attach them with strong reference:

stage.addEventListener(MouseEvent.CLICK, clickHnd, false, 1, false); // 5th parameter is set to 'false', not 'true'.


Higher priorities (larger numbers) will get fired first. Simple! In the above code, clickHnd2() will be processed before clickHnd().  (There is a priority 3 event listener, but it is a duplicate, so doesn’t actually exist).


If anything interesting (to me!) comes up about events, I will add on to this post.

– 30 –