AS3 - Event listener that only fires once

Posted by Zed-K on Stack Overflow See other posts from Stack Overflow or by Zed-K
Published on 2010-03-19T10:14:04Z Indexed on 2010/03/22 17:01 UTC
Read the original article Hit count: 439

Filed under:
|
|
|

I'm looking for a way to add an EventListener which will automatically removes itself after the first time it fires, but I can't figure a way of doing this the way I want to.

I found this function (here) :

public class EventUtil
{
    public static function addOnceEventListener(dispatcher:IEventDispatcher,eventType:String,listener:Function):void
    {
         var f:Function = function(e:Event):void
         {
              dispatcher.removeEventListener(eventType,f);
              listener(e);
          }
          dispatcher.addEventListener(eventType,f);
    }
}


But instead of having to write :

EventUtil.addOnceEventListener( dispatcher, eventType, listener );

I would like to use it the usual way :

dispatcher.addOnceEventListener( eventType, listener );


Has anybody got an idea of how this could be done?
Any help would be greatly apprecitated.


(I know that Robert Penner's Signals can do this, but I can't use them since it would mean a lot of code rewriting that I can't afford for my current project)

© Stack Overflow or respective owner

Related posts about as3

Related posts about addeventlistener