2 Replies Latest reply on Oct 18, 2007 8:42 AM by ian_c_c

    Problem removing listener after reload

      Note: Using AS2, Flash 8

      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 game)
      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 name?
      How do I make sure that there is only one listener active?

        • 1. Re: Problem removing listener after reload
          clbeech Level 3
          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:

          Hey, what did I win! ;)

          • 2. Re: Problem removing listener after reload
            ian_c_c Level 1
            You are the winner!!

            Seriously, if I could give you a prize, I would. That fixed the problem right away.

            Thank you. Thank you. Have a beer: