2 Replies Latest reply on Oct 15, 2014 9:38 AM by Amy Blankenship

    Flash using arrow keys to move between frames in an animation

    Dr Technology

      Hi,

      Im currently creating a calculator on flash and I would like to be able to move to the next part of the calculator (the next frame) by using the arrow keys on my keyboard

      This is my current code to move between the frames using a buttons.

       

      stop();

       

       

      var input1:String;

      var input2:String;

      var Res:Number;

      var Quote:Number;

       

      txtLen.restrict = "0-9"

      txtWid.restrict = "0-9"

       

       

      BtnCon2.addEventListener(MouseEvent.CLICK,fnCall)

       

       

      function fnCall(Event:MouseEvent):void{

       

        input1 = txtLen.text;

        input2 = txtWid.text;

        Res = parseInt(input1) * parseInt(input2);

        Res.toString();

        gotoAndPlay(21)

        txtArea.text = String(Res);

       

      }

       

       

      BtnRes1.addEventListener(MouseEvent.CLICK,fnRes1)

       

       

      function fnRes1(Event:MouseEvent):void{

        gotoAndPlay(1);

       

      Im trying to do the same thing that im doing with my buttons but with my arrow keys

       

      Thanks

        • 1. Re: Flash using arrow keys to move between frames in an animation
          Ned Murphy Adobe Community Professional & MVP

          Here is a somewhat generic bit of code involving the arrows keys....

           

          function hearKeyUp(yourEvent:KeyboardEvent):void{    
               if (yourEvent.keyCode==Keyboard.RIGHT){  processing code goes here }   
               if (yourEvent.keyCode==Keyboard.LEFT){  processing code goes here }      
               if (yourEvent.keyCode==Keyboard.UP){  processing code goes here } 
               if (yourEvent.keyCode==Keyboard.DOWN){  processing code goes here }
          }


          stage.addEventListener(KeyboardEvent.KEY_UP, hearKeyUp);

          • 2. Re: Flash using arrow keys to move between frames in an animation
            Amy Blankenship Level 4

            Another way to write what Ned said is instead of multiple if's, you could do something like:

             

            switch(yourEvent.keyCode) {

                 case Keyboard.RIGHT:

                      fnCall(null);

                      break;

                 case Keyboard.LEFT:

                      fnRes1(null);

                      break;

                 case Keyboard.UP:

                      //do up stuff

                      break;

                 case Keyboard.DOWN:

                      //do down stuff

                      break;

                 default:

                      trace('I\'m confused by this key.');

                      break;

            }

            Some people find this syntax more legible.