Alex Rivera | Logout

remove a listener that has an anonymous function in Actionscript 3

Asked 2010-01-06T12:58:56.367
11
up.addEventListener(MouseEvent.CLICK, 
    function clickFunc(event:MouseEvent):void
    { 
        revealSpinner(event,51.42,1,spinner);
        event.currentTarget.removeEventListener(event.type, arguments.callee);
        autoTimer.stop();
    },
    false, 0, true);
down.addEventListener(MouseEvent.CLICK, 
    function clickFunc(event:MouseEvent):void
    { 
        revealSpinner(event,51.42,-1,spinner);
        event.currentTarget.removeEventListener(event.type, arguments.callee);
        autoTimer.stop();
    },
    false, 0, true);

The above code adds a listener to a couple of MC's. Originally the methods were anonymous but I have named them clickFunc() in order to try an reference them in my remove listener.

Here is my remove listener code. Both of these snippets are in separate functions. The add listener method is called before the remove method.

up.removeEventListener(MouseEvent.CLICK, clickFunc );
down.removeEventListener(MouseEvent.CLICK, clickFunc);

As soon as I publish the movie I get this error:

1120: Access of undefined property clickFunc.
Edit
Report

1 Answer

4

ok there is a few ways that you can do this. here is the first way which i think is what your looking for.

var upAnon:Function;
var downAnon:Function;

up.addEventListener(MouseEvent.CLICK, 
    upAnon = function (event:MouseEvent):void
    { 
        revealSpinner(event,51.42,1,spinner);
        event.currentTarget.removeEventListener(event.type, arguments.callee);
        autoTimer.stop();
    },
    false, 0, true);
down.addEventListener(MouseEvent.CLICK, 
    downAnon = function (event:MouseEvent):void
    { 
        revealSpinner(event,51.42,-1,spinner);
        event.currentTarget.removeEventListener(event.type, arguments.callee);
        autoTimer.stop();
    },
    false, 0, true);

up.removeEventListener(MouseEvent.CLICK, upAnon);
down.removeEventListener(MouseEvent.CLICK, downAnon);

and here is another way, getting the same desired effect and functionality as the first. just cleaner and usually standard practice.

function upFunction(event.MouseEvent):void {
    revealSpinner(event,51.42,1,spinner);
    event.currentTarget.removeEventListener(event.type, arguments.callee);
    autoTimer.stop();
}
function downFunction(event.MouseEvent):void {
    revealSpinner(event,51.42,-1,spinner);
    event.currentTarget.removeEventListener(event.type, arguments.callee);
    autoTimer.stop();
}

up.addEventListener(MouseEvent.CLICK, upFunction, false, 0, true);
down.addEventListener(MouseEvent.CLICK, downFunction, false, 0, true);

up.removeEventListener(MouseEvent.CLICK, upFunction);
down.removeEventListener(MouseEvent.CLICK, downFunction);

The reason that you would use the first type, is if you were creating the eventHandler inside a function and need to use variables defined in that function, inside your anonymous function. for example, here im accessing the var countMe inside the anon function and incrementing it on mouse_down, and it will increment until the mouse_up is activated. so every time

answered 2010-08-04T02:32:39.417

Your Answer