3 Replies Latest reply on Feb 17, 2014 10:46 AM by AdeptDigital

    Having a case Keyboard.UP and .LEFT issue

    AdeptDigital Level 2

      Hello,

       

      Putting together a simple slide show and looking to tie the slides to the arrow keys when an odd thing happened. I can move forward through the program but not backward. This issue is happening with both the Up and LEFT arrows which I want to move back through the program while the RIGHT and DOWN arrow move forward. Simple stuff right? But the LEFT and UP arrows do not shift the frames. The trace is showing me that the buttons are being pressed just like the DOWN and RIGHT arrows but the functions are not being completed. I thought the problem was with the prevFrame(); since it was the obvious difference between the lines of code.

       

      stop();

       

       

      stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

       

       

      function fl_KeyboardDownHandler(event:KeyboardEvent):void

      {

                trace("Key Code Pressed: " + event.keyCode);

        switch (event.keyCode)

                {

                          case Keyboard.UP:

                          {

                                    if(currentFrame == 1){

                          gotoAndStop(10); }

                                    else{                     

                          prevFrame(); }

                          }

       

                          case Keyboard.DOWN:

                          {

                                    if(currentFrame == 10){

                          gotoAndStop(1); }

               else{

                          nextFrame(); }

                          }

       

                   case Keyboard.LEFT:

                          {

                                    if(currentFrame == 1){

                          gotoAndStop(10); }

                                    else{

                          prevFrame(); }

                          }

       

                          case Keyboard.RIGHT:

                          {

                                    if(currentFrame == 10){

                          gotoAndStop(1); }

                else{

                          nextFrame(); }

                          }

                }

      }

       

       

       

      Then I thought to switch around the code corresponding to the LEFT and RIGHT arrows and found that it was not the prevFrame(); that I thought it was. No, the buttons themselves are not responding, here is the changed code.

       

      stop();

       

       

      stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

       

       

      function fl_KeyboardDownHandler(event:KeyboardEvent):void

      {

                trace("Key Code Pressed: " + event.keyCode);

        switch (event.keyCode)

                {

                          case Keyboard.UP:

                          {

                                    if(currentFrame == 1){

                          gotoAndStop(10); }

                                    else{                     

                          prevFrame(); }

                          }

       

                          case Keyboard.DOWN:

                          {

                                    if(currentFrame == 10){

                          gotoAndStop(1); }

               else{

                          nextFrame(); }

                          }

       

        case Keyboard.LEFT:

                          {

                                    if(currentFrame == 10){

                          gotoAndStop(1); }

                                    else{

                          nextFrame(); }

       

       

                          }

       

                          case Keyboard.RIGHT:

                          {

                                    if(currentFrame == 1){

                          gotoAndStop(10); }

                                    else{

                          prevFrame(); }

                          }

                }

      }

      When I swapped the actions connected to the LEFT arrow and the RIGHT arrow, the RIGHT arrow activated the prevFrame(); but the LEFT arrow was now dead. Something is preventing AS3 from triggering the action on the LEFT arrow and UP arrow that is not preventing it from happening on the DOWN and RIGHT arrow presses. Anyone have an idea as to why?

       

      Thanks for your thoughts,

      Brett

        • 1. Re: Having a case Keyboard.UP and .LEFT issue
          AdeptDigital Level 2

          While writing the above I got the idea to trace the button actions like this:

           


             case Keyboard.LEFT:


          {



          if(currentFrame == 1){

                              gotoAndStop(10); }




          else{

                              prevFrame(); }




          trace(currentFrame);


          }





          case Keyboard.RIGHT:


          {



          if(currentFrame == 10){

                              gotoAndStop(1); }

                    else{

                              nextFrame(); }




          trace(currentFrame);


          }

           

          The result showed the movement of the playhead on the frames and it showed that when I hit the RIGHT arrow the frame advanced as it should but when I hit the LEFT arrow for the prevFrame(); it went back then forward again:

           

          Key Code Pressed: 39

          2

          Key Code Pressed: 39

          3

          Key Code Pressed: 39

          4

          Key Code Pressed: 39

          5

          Key Code Pressed: 37

          4

          5

          Key Code Pressed: 37

          4

          5

          Key Code Pressed: 37

          4

          5

          Key Code Pressed: 37

          4

          5

          This is the output showing the first four keystrokes of the RIGHT arrow moving forward then the next four keystrokes of the LEFT arrow moving backward then forward with each keystroke.

           

          Any Thoughts?

           

          Thanks.

          • 2. Re: Having a case Keyboard.UP and .LEFT issue
            AdeptDigital Level 2

            Just because this may help I rearranged the case Keyboard functions to get the RIGHT and LEFT arrows to work but the UP and DOWN arrows do not:

             

            stop();

             

             

            stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

             

             

            function fl_KeyboardDownHandler(event:KeyboardEvent):void

            {

                      trace("Key Code Pressed: " + event.keyCode);

                      switch (event.keyCode)

                      {

                                case Keyboard.UP:

                                {

                                          if(currentFrame == 1){

                                gotoAndStop(10); }

                                          else{                     

                                prevFrame(); }

                                }

                                case Keyboard.RIGHT:

                                {

                                          if(currentFrame == 10){

                                gotoAndStop(1); }

                      else{

                                nextFrame(); }

                                }

                                case Keyboard.DOWN:

                                {

                                          if(currentFrame == 10){

                                gotoAndStop(1); }

                     else{

                                nextFrame(); }

                                }

               case Keyboard.LEFT:

                                {

                                          if(currentFrame == 1){

                                gotoAndStop(10); }

                                          else{

                                prevFrame(); }

                                }

                                          trace(currentFrame);

                      }

            }

            So since this worked for the LEFT and RIGHT arrows but not the UP and DOWN I thought to remove the Up and DOWN to see what would happen. When I did the RIGHT arrow stopped working while the LEFT arrow did. It has something to do with the order of the case Keyboards but I still do not understand how.

             

            Something is limiting the way the code works for the arrows, I don't know enough about the switch (event.keyCode) to be able to say that the problem starts there but it has to be something.

             

            Thanks

            • 3. Re: Having a case Keyboard.UP and .LEFT issue
              AdeptDigital Level 2

              Fixed it.  The issue was that the individual keystrokes were interfering with each other. If anyone is interested here is the repaired code. The trick is to put in a break: after each one.

               

              stop();

               

               

              stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

               

               

              function fl_KeyboardDownHandler(event:KeyboardEvent):void

              {

                        trace("Key Code Pressed: " + event.keyCode);

                        switch (event.keyCode)

                        {

                                  case Keyboard.UP:

                                  {

                                            if(currentFrame == 1){

                                  gotoAndStop(10); }

                                            else{                     

                                  prevFrame(); }

                                            break;

                                  }

                                  case Keyboard.RIGHT:

                                  {

                                            if(currentFrame == 10){

                                  gotoAndStop(1); }

                                            else{

                                  nextFrame(); }

                                            break;

               

               

                                  }

                                  case Keyboard.DOWN:

                                  {

                                            if(currentFrame == 10){

                                  gotoAndStop(1); }

                                            else{

                                  nextFrame(); }

                                            break;

               

               

                                  }

                                  case Keyboard.LEFT:

                                  {

                                            if(currentFrame == 1){

                                  gotoAndStop(10); }

                                            else{

                                  prevFrame(); }

                                            break;

                                  }

                                            trace(currentFrame);

                        }

              }