Home > actionscript > handy way of removing event listeners via arguments.callee

handy way of removing event listeners via arguments.callee

Theo’s entry reminded me how useful this can be especially when using an anonymous function as a one time callback. Try it out:

stage.addEventListener(MouseEvent.MOUSE_DOWN, function(event:Event):void { event.currentTarget.removeEventListener(event.type, arguments.callee); trace(‘ran once’); });

FYI you should always use event.currentTarget and not event.target when removing a mouse event listener because event.currentTarget will always reference the object that the listener was explicitly attached to. event.target can easily reference a different object like a child displayObject inside the displayObject you were listening for a bubbling event on.

Categories: actionscript Tags:
  1. No comments yet.
  1. No trackbacks yet.