2 Replies Latest reply on Mar 4, 2008 2:00 PM by Mr.Wallace

    Adding & Removing Listeners

    Mr.Wallace
      Well, I've been searching this site for a little while trying to wrap my head around my problem but to no luck. My apologies if this is a repetitve subject or if it's kind of "newbie"-ish of me but, I am in a jam over here.

      Basically, I'm trying to go from frame 1 to frame 2 to frame 3 (for example) using certain keystrokes. Starting from frame 1 my code looks like this:

      keyListener = new Object();
      keyListener.onKeyDown = function() {
      if (Key.isDown(85)) {
      gotoAndPlay("2");
      }
      };
      Key.addListener(keyListener);


      ( 85 is the key code for the letter "c")

      And that works fine. But, now that I'm on frame 2, I want a different keystroke to send me to frame 3. So my code looks like this:

      keyListener = new Object();
      keyListener.onKeyDown = function() {
      if (Key.isDown(80)) {
      gotoAndPlay("3");
      }
      };
      Key.addListener(keyListener);


      ( 80 is the key code for the letter "u")

      And that works too. However, the previous keystroke (85 or "c") will send me to frame 3 as well. I only want one keystroke to send me to frame 3, not both. I've tried looking around the site to see if a removeListener would work but I can't figure it out.

      Any ideas? I could really use the help.

      THANKS in advance!!!
        • 1. Adding & Removing Listeners
          clbeech Level 3
          Mr.W - sorry for not getting back with to your question the other day, got tied up. But I also would 'steer clear' of the F keys, they are often used for 'native' functions on the users system and could become problematic.

          as to the post here today, you simply need to use a 'single' listener and then check for different keys with the conditional statement, you're close. I also note that the key code for the 'c' key is 67 not 80, and that it seems like a gotoAndStop() method would be better, also you only need a number without the quote when calling to a frame number :)

          In situations like this, I like to use a 'switch' statement which acts similar to the 'if - else' statements, but is a little 'cleaner', so it would be something like this:
          • 2. Re: Adding & Removing Listeners
            Mr.Wallace Level 1
            Hey cl,

            No problems about yesterday, totally understand. I did eventually figure out the key codes and scripting whatnot that I needed in order to make the F keys operational. Thanks!

            As for this topic and your suggestions, I will definately take a look at it in more depth tomorrow and let you know how it goes.

            I really appreciate you taking the time to help me out, thanks!