Skip to main content
Inspiring
April 2, 2016
Answered

how to dynamically remove event listeners ?

  • April 2, 2016
  • 1 reply
  • 549 views

I have a graphic keypad made up of key movie clips.  When a key is pressed a function is called and actions are performed. Afterwards I want to disable whichever key was pressed by removing the listener. How can I do that dynamically using a string for the listener function name?

Enables a key

keyK.addEventListener(MouseEvent.CLICK, insertConsK);

Disable by removing listener.  What is the correct syntax? char="k".

MovieClip(root)["key"+char.toUpperCase()].removeEventListener(MouseEvent.CLICK, ["insertCons"+char.toUpperCase()] );

This topic has been closed for replies.
Correct answer kglad

this['key'+char.toUpperCase()].removeEventListener(MouseEvent.CLICK,this['InsertCons'+char.toUpperCase()])

but if each key has it's own listener function, just use e.currentTarget in the listener function and hardcord the function name.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 2, 2016

this['key'+char.toUpperCase()].removeEventListener(MouseEvent.CLICK,this['InsertCons'+char.toUpperCase()])

but if each key has it's own listener function, just use e.currentTarget in the listener function and hardcord the function name.

Inspiring
April 2, 2016

each key has its own listener. Your second suggestion works.  Thanks!

evt.currentTarget.removeEventListener(MouseEvent.CLICK, keyHandlerVowel);

kglad
Community Expert
Community Expert
April 2, 2016

you're welcome.

p.s. it would have been easier to have them all use the same listener function but since you already have all those functions defined, it's probably easiest to leave things as they are.