Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

removeEventListener not working

Explorer ,
Apr 01, 2016 Apr 01, 2016

I'm having trouble removing event listeners from movie clips.  As a test I put two MCs on stage: keyB and remover.


I want to pass  a parameter to the keyB event listener function.  This works fine.  But when I try to remove the listener it doesn't work.

keyB.addEventListener(MouseEvent.CLICK, function (evt:MouseEvent):void {keyHandler(evt,"b")});

remover.addEventListener(MouseEvent.CLICK, removeHandler);

function keyHandler(evt:MouseEvent, char:String):void {

trace(char);

}

function removeHandler(evt:MouseEvent):void {

keyB.removeEventListener(MouseEvent.CLICK, function (evt:MouseEvent):void {keyHandler(evt,"b")});

keyB.alpha=.5

}

TOPICS
ActionScript
768
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Apr 01, 2016 Apr 01, 2016

Found the solution here:

http://stackoverflow.com/questions/13486230/to-pass-a-parameter-to-event-listener-in-as3-the-simple-way-does-it-exist/13488640#13488640

//you can pass parameter to the function this way:

stage.addEventListener(MouseEvent.CLICK, onClick(true, 123, 4.56, "string"));

function onClick(b:Boolean, i:int, n:Number, s:String):Function {

  return function(e:MouseEvent):void {

    trace("Received " + b + ", " + i + ", " + n + " and " + s + ".");

  };

}

//But to be able to remove the listen

...
Translate
Explorer ,
Apr 01, 2016 Apr 01, 2016
LATEST

Found the solution here:

http://stackoverflow.com/questions/13486230/to-pass-a-parameter-to-event-listener-in-as3-the-simple-...

//you can pass parameter to the function this way:

stage.addEventListener(MouseEvent.CLICK, onClick(true, 123, 4.56, "string"));

function onClick(b:Boolean, i:int, n:Number, s:String):Function {

  return function(e:MouseEvent):void {

    trace("Received " + b + ", " + i + ", " + n + " and " + s + ".");

  };

}

//But to be able to remove the listener you store the function in a variable that is a function = the function call in the listener.

//then call that function in the listener.

var functionOnClick:Function = onClick(true, 123, 4.56, "string");

stage.addEventListener(MouseEvent.CLICK, functionOnClick);

function onClick(b:Boolean, i:int, n:Number, s:String):Function {

  return function(e:MouseEvent):void {

    trace("Received " + b + ", " + i + ", " + n + " and " + s + ".");

  };

}

trace("Before: " + stage.hasEventListener(MouseEvent.CLICK));

stage.removeEventListener(MouseEvent.CLICK, functionOnClick);

trace("After: " + stage.hasEventListener(MouseEvent.CLICK));

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines