Copy link to clipboard
Copied
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['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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
each key has its own listener. Your second suggestion works. Thanks!
evt.currentTarget.removeEventListener(MouseEvent.CLICK, keyHandlerVowel);
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now