5 Replies Latest reply on Mar 28, 2011 12:47 PM by Ned Murphy

    Key Listeners

    Kents79 Level 1

      Hi

       

      I'm trying to use Key Listeners to solve my problem.

      I don't know much about key liseners so can somebody help me out.

       

      I have the following on a keyframe yet it still wont jump to the certain frames when I press the relevent keys. Am I missing something?

       

      stop();

      KeyListener = new Object();
      KeyListener.onKeyDown = function() {

      if (Key.getAscii() == 38) {  // UP key
      gotoAndStop(89);}

      if (Key.getAscii() == 40) {  // DOWN key
      gotoAndStop(91);}

      if (Key.getAscii() == 37)  // LEFT key
      gotoAndStop(88);}

      if (Key.getAscii() == 32) {  // SPACEBAR key
      loadMovie("application_01.swf", "container");}

       

      Its says I should place the following after the function or on another frame where you want the key listender to start :

      Key.addListener(KeyListener);

       

      I don't under stand this................

       

      Can anybody help??

       

       

       

       

       

       

       

       

        • 1. Re: Key Listeners
          Ned Murphy Adobe Community Professional & MVP

          It's telling you that you need to assign the listener to the Key object.  Also, for the values you are testing for you should use getCode instead of getAscii...

           

          var keyListener:Object = new Object();
          Key.addListener(keyListener);

           

          keyListener.onKeyDown = function(){
              if(Key.getCode() == 38) gotoAndStop(89);
              if(Key.getCode() == 40) gotoAndStop(91);
              if(Key.getCode() == 37) gotoAndStop(88);
              if(Key.getCode() == 32) loadMovie("application_01.swf", "container");
          }

          • 2. Re: Key Listeners
            Kents79 Level 1

            How do I assign the listener to theKey object?

             

            And id I have the above code in a few frames along the timeline, will it slow down the swf?

            • 3. Re: Key Listeners
              Ned Murphy Adobe Community Professional & MVP

              It is included in the code I showed and the message you say you received earlier... Key.addListener(keyListener);

               

              Why would you need to have the code in a few frames along the timeline?  You can assign that code in frame 1 of an actions layer and it will remain effective down the timeline without having to repeat it.

              1 person found this helpful
              • 4. Re: Key Listeners
                Kents79 Level 1

                I have it repeated down the timeline because:

                 

                I have a menu with 3 buttons.

                 

                There's a stop command on each frame.

                Frame1: Button 1 highlighted - Down key would bring you to Frame 2.

                Frame2: Button 2 highlighted -Up key would bring you to Frame1. Down key would bring you to Frame 3.

                Frame3: Button 3 highlighted - Up key would bring you to Frame2.

                 

                Is there any other way I could do it?

                • 5. Re: Key Listeners
                  Ned Murphy Adobe Community Professional & MVP

                  You can probably work out another approach that uses just one set of code and the prevFrame() and nextFrame() functions instead of using specific frame numbers.

                  1 person found this helpful