Denoteone
Denoteone

Reputation: 4055

Remove eventListener not working on buttons AS3 - Flash

I am trying to remove an eventlisnter on a button so when the button is pressed the animation completes before you can press the button again. But based on my code below you can push the button as many times as you like:

var LeftButt:MovieClip  = new left_button();
var RightButt:MovieClip  = new right_button();
var topClip:Sprite = new Sprite();
addChild(topClip);
     LeftButt.addEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "left");});
     RightButt.addEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "right");});  


function clothingApp(event:MouseEvent):void{
     topClip.addChild(RightButt);
     topClip.addChild(LeftButt);

}

function moveItems(event:MouseEvent, SlideDirection:String):void{

    LeftButt.removeEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "left");});
    RightButt.removeEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "right");});    

    trace(SlideDirection);
}

So technically this code should only run once because I never set up the eventListener again. But you can press the buttons as many times as you like.

Upvotes: 1

Views: 2948

Answers (1)

Sam DeHaan
Sam DeHaan

Reputation: 10325

If you want to remove event listeners, you can't add them using anonymous functions.

Create a wrapper function with the same functions as your anonymous function, and you'll be fine.

function moveLeft(event:MouseEvent):void
{
    moveItems(event, "left");
}

function moveRight(event:MouseEvent):void
{
    moveItems(event, "right");
}


LeftButt.addEventListener(MouseEvent.MOUSE_UP, moveLeft);
RightButt.addEventListener(MouseEvent.MOUSE_UP, moveRight);   

LeftButt.removeEventListener(MouseEvent.MOUSE_UP, moveLeft);
RightButt.removeEventListener(MouseEvent.MOUSE_UP, moveRight);  

Upvotes: 4

Related Questions