Marty
Marty

Reputation: 39476

AS3 referencing current function

I just came across a curious scenario where I want to use removeEventListener() within a function that doesn't have a name. By this I mean, I've created the function within addEventListener(), instead of making reference to one:

addEventListener(
    Event.ENTER_FRAME, 
    function(e:Event):void
    {
        if(getTimer() > 8000)
        {
            // removeEventListener(Event.ENTER_FRAME, <<this function>>);
            // Other stuff
        }
    }
);

Is it possible to make a reference to the current function (ie the function I'm working within)? Or do I just need to structure the above the standard way?

Please not that I am fully aware that you can use many of the standardized methods available to achieve the above, it was purely an example snippet.

Upvotes: 2

Views: 2409

Answers (3)

Mark Knol
Mark Knol

Reputation: 10143

Using anonymous functions in actionscript is a bad choice, since it is really slow. Also they hardly can be garbage collected. It is also good to mention that this will only work if when the listener has been called (yes, in case of an enter_frame it will), so outside the anonymous function other functions are unable to remove the listener. Beside that, is is also a actionscript-convention to use separate functions, which makes your code more readable and it will take only a few extra chars (just to name it).

addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(e:Event):void
{
    if(getTimer() > 8000)
    {
         removeEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
}

If you want a more easy way to remove the event listener; you could detect the type and the function callee of the listener from the target object. However I think this also makes the code a bit less readable.

e.target.removeEventListener(e.type, arguments.callee);

sources
http://jacksondunstan.com/articles/413
http://gskinner.com/blog/archives/2006/07/as3_weakly_refe.html

Upvotes: 2

cwallenpoole
cwallenpoole

Reputation: 82088

There are two options, you can either give it a name (and there are three ways to do that) or you can use arguments.callee.

In the case of the former, the three ways to name a function in AS3:

class Foo
{
    // class (static or member) level
    public function bar():void
    {
       // use a variable (technically, this function is anonymous, but we can
       // still use the variable to reference the function itself.
       var inVariable:Function = function():void
       {
          // declare it in a local scope
          function local():void
          {

          }
       }
    }
}

To use a named function:

function callback(e:Event):void {
    trace("tick");
    removeEventListener(Event.ENTER_FRAME, callback);
}
addEventListener(Event.ENTER_FRAME, callback);

To use arguments.callee:

addEventListener(
    Event.ENTER_FRAME, 
    function(e:Event):void
    {
        if(getTimer() > 8000)
        {
            // I get superstitious and I use a local variable.
            var callee:Function = arguments.callee
            removeEventListener(event.type, callee);
            // Other stuff
        }
    }
);

Upvotes: 15

Tyler Egeto
Tyler Egeto

Reputation: 5495

You just need to give it a name, eg:

addEventListener(Event.ENTER_FRAME, function callback(e:Event):void {
    trace("tick");
    removeEventListener(Event.ENTER_FRAME, callback);
});

In this example "tick" will only be traced one time.

Upvotes: 2

Related Questions