Here's the setup:
I'm developing a small game based on Wheel of Fortune. The
.swf for the game is loaded into another flash application on
another developer's end. Their application is build like a
slideshow. (switching from one .swf to the next, one of which is my
I have a "board" of letter spaces and an input text field for
guessing the phrase.
The user can either hit letter keys to guess letters on the
board OR click in the input field and guess the phrase. The input
field has onSetFocus and onKillFocus function to prevent both
happening at the same time. Once a user has entered text in the
field, they can either hit a button or press enter to check if it's
the right answer.
Attached at the bottom is a simplified example of the
listeners for key presses and the input field's functions:
Thankfully, all this code works fine, but only the first time
around. If you go back to the game again, it doesn't work the same:
If you start typing in the input field, letters pop up on the board
too. (Which, of course, is not supposed to happen)
I've been able to test this by testing the movie (Ctrl-Enter)
and then hitting Ctrl-Enter again to reload.
After doing a trace(Key._listeners.length); I get 1 the first
time and 2 the second time.
What this tells me is that on the second time around, even
though the input_txt.onSetFocus function should be removing the
keyListener, the keyListener.onKeyDown function is still executing
because there are now 2 keyListeners. (1 is removed, but the other
continues to execute)
I've tried attempting to remove the keyListener before any of
the code above executes, but it seems to have no effect.
How is it possible for there to be 2 listeners by the same
How do I make sure that there is only one listener active?
This is an interesting problem. The Key._listener array must
still live in the cache while Flash is in operation, it seems if
you close down the program and then reopen (like if you shut down
the browser and came back) this doesn't happen, but on reload it
most certainly does.
I have a solution for you. Loop through the array and remove
all registered listeners on reload, place the code below previous
to the registering of the 'keyListener' object: