views:

677

answers:

1

I need to disable an onClick action until an animation has stopped. Animations can be started by 4 different buttons - all need to be deactivated.

I use a listener to pass params to the function that will initiate the animation which is why I use an anonymous function in the add listener:

up.addEventListener(MouseEvent.CLICK, 
    function(event:MouseEvent):void
    { 
        revealSpinner(event,51.42,1,spinner);
        event.currentTarget.removeEventListener(event.type, arguments.callee);
    },
    false, 0, true);

I also have a self calling remove listener, but really I need to remove the listener from the other 3 buttons.

I have also tried naming the anonymous function but that didn't work:

up.addEventListener(MouseEvent.CLICK, 
    myFunc = function(event:MouseEvent):void
    { 
        revealSpinner(event,51.42,1,spinner);
    },
    false, 0, true);

// somewhere else in my app
up.removeEventListener(MouseEvent.CLICK, myFunc ); 

Edit: each of the 4 buttons has to pass different parameters to the revealSpinner() method revealSpinner(event,51.42,1,spinner); revealSpinner(event,51.42,-1,spinner); revealSpinner(event,120,1,anotherMC); revealSpinner(event,120,-1,anotherMC);

A: 

So you have 4 buttons total. Each button runs a different animation on the "spinner" and you want to disable the CLICK event on each of the 4 buttons any time one of them is clicked, correct?

If so:

var buttons:Dictionary= new Dictionary(true);
buttons[up] = handleUpClick;
buttons[down] = handleDownClick;
buttons[left] = handleLeftClick;
buttons[right] = handleRightClick;

initClickEvents();

function initClickEvents() {
    for (var i:TypeIfYouHaveIt in buttons) {
        this[i].addEventListener(MouseEvent.CLICK, buttons[i], false, 0, true);
    }
}

function killClickEvents() {
    for (var i:TypeIfYouHaveIt in buttons) {
        this[i].removeEventListener(MouseEvent.CLICK, buttons[i]);
    }
}

function handelUpClick($evt:MouseEvent):void {
    revealSpinner($evt, 51.42, 1, spinner);
    killClickEvents();
}

function handleDownClick($evt:MouseEvent):void {
    ...
    ...
}
...
...

Once your animation is complete, call the initClickEvents function.


You could also use a method that works just as well, have a sprite with 0 opacity that is placed over the buttons. When animation begins, coverSprite.visible = true, when animation ends, coverSprite.visible = false.

sberry2A
each of the 4 buttons has to pass different parameters, hence the need for an anon function -> to call the intended function in the add listener. Otherwise I have to create a custom event listener. It's the passing of those params that's complicating the issue.
ed209