4 Replies Latest reply on Aug 27, 2009 9:03 AM by kglad

    Problem with Conditional Logic within KeyListener.

    JeremySeda

      I'm writing a KeyListner that will navigate the timeline of my Flash movie based on what arrow key you press:

       

      • UP arrow key should gotoAndStop(1);.
      • DOWN arrow key should gotoAndStop(50);.
      • RIGHT arrow key should nextFrame();.
      • LEFT arrow key should prevFrame();.

       

      The RIGHT and LEFT arrow keys work perfectly, so long as you don't hit the DOWN or UP keys and use the LEFT arrow key to go before the first frame or RIGHT arrow key to go after frame 50.  Does this problem sound familiar to anyone and do you have advice on how to resolve this issue?  Here's my Listener Code in the first Frame of my Flash movie:

       

      var myListener:Object = new Object();
      myListener.onKeyDown = function() {
          if (Key.getCode() == Key.RIGHT)) {
              trace("You pressed the right arrow key");
        nextFrame();
          } else if (Key.getCode() == Key.LEFT) {
        trace("You pressed the left arrow key");
        prevFrame();
      } else if (Key.getCode() == Key.UP) {
        trace("You pressed the up arrow key");
        gotoAndStop("First Frame");
      } else if (Key.getCode() == Key.DOWN) {
        trace("You pressed the down arrow key");
        gotoAndStop("Last Frame");
      } else if (Key.getCode() == Key.SPACE) {
        trace("You pressed the space key");
      }
      }
      Key.addListener(myListener);

        • 1. Re: Problem with Conditional Logic within KeyListener.
          kglad Adobe Community Professional & MVP

          only execute the nextFrame() if the _currentframe is less than 50 and only execute prevFrame() if the _currentframe is greater than 1.

          1 person found this helpful
          • 2. Re: Problem with Conditional Logic within KeyListener.
            JeremySeda Level 1

            This gets jacked up if you go to the last frame (DOWN) and/or back to the first frame (UP) and then try to navigate with the LEFT and RIGHT buttons.  Once you jump to the last or first frame and toggle between the LEFT and RIGHT, you’ll see that instead of sequentially advancing things get a little dodgy—DOWN (frame 50), UP (frame 1), RIGHT (frame 2), LEFT (frame 1), RIGHT (frame 3), LEFT (frame 1), RIGHT (frame 4), LEFT (frame 1).

            • 3. Re: Problem with Conditional Logic within KeyListener.
              JeremySeda Level 1

              I had a buddy of mine take a look and figured out a solution.  Just in case anyone else runs into this same problem, here's the code he revised:

               

              trace("Now at:"+this._currentframe);

              if (myListener==undefined) {
              var myListener:Object = new Object();
              myListener.onKeyDown = function() {
                switch (Key.getCode()) {
                 case Key.RIGHT:
                  trace("You pressed the right arrow key");
                  if (_currentframe < 50){
                   trace("Moving away from Frame:"+_currentframe);
                   trace("---------------------------------------");
                   nextFrame();
                  }
                  break;
                 case Key.LEFT:
                  trace("You pressed the left arrow key");
                  if(_currentframe > 1){
                   trace("Moving away from Frame:"+_currentframe);
                   trace("---------------------------------------");
                   prevFrame();
                  }
                  break;
                 case Key.UP:
                  trace("You pressed the up arrow key");
                  trace("Moving away from Frame:"+_currentframe);
                  trace("---------------------------------------");
                  gotoAndStop("First Frame");
                  break;
                 case Key.DOWN:
                  trace("You pressed the down arrow key");
                  trace("Moving away from Frame:"+_currentframe);
                  trace("---------------------------------------");
                  gotoAndStop("Last Frame");
                  break;
                 case Key.SPACE:
                  trace("You pressed the space key");
                  trace("---------------------------------------");
                  break;
                 default:
                  trace("You pressed "+Key.getCode());
                  trace("---------------------------------------");
                  break;
                }
              }

              Key.addListener(myListener);
              }
              stop();

               

              And on each frame that the arrow keys navigated to, he added a simple trace statement:

               

              trace("Now at:"+this._currentframe);

              • 4. Re: Problem with Conditional Logic within KeyListener.
                kglad Adobe Community Professional & MVP

                except for the trace() functions, isn't that what i suggested?