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

how to dynamically remove event listeners ?

Explorer ,
Apr 02, 2016 Apr 02, 2016

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()] );

TOPICS
ActionScript
497
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

Community Expert , Apr 02, 2016 Apr 02, 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.

Translate
Community Expert ,
Apr 02, 2016 Apr 02, 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.

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
Explorer ,
Apr 02, 2016 Apr 02, 2016

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

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

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
Community Expert ,
Apr 02, 2016 Apr 02, 2016
LATEST

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.

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