1 Reply Latest reply on Sep 13, 2009 4:47 PM by Ned Murphy

    Arrow Movements In Flash CS4

    Me2LoveIt2

      Hi all I just made a new Flash project.
      It lets you move a Cube, with the arrow keys on the keyboard.
      This is the code.

      Code:
      function hearKey(yourEvent:KeyboardEvent):void{
           if (yourEvent.keyCode==Keyboard.RIGHT){
                cube_mc.x+=5
           };
           if (yourEvent.keyCode==Keyboard.LEFT){
                cube_mc.x-=5
           };
           if (yourEvent.keyCode==Keyboard.UP){
                cube_mc.y-=5
           };
           if (yourEvent.keyCode==Keyboard.DOWN){
                cube_mc.y+=5
           };
      };
      stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKey);

      It works perfectly fine, except for example:

      I press down the down key...

      it moves down...(good so far)

      I keep pressing the down key down and also press the left key down...

      now it moves left...(not so good, i want it to move both left and down, how do i do that?)

      now i release the left key but am still on the down key....

      nothing happens....(bad, i want it to then continue going down)

      I hope someone understands what I'm asking.
      Thank you for the reply in advance

        • 1. Re: Arrow Movements In Flash CS4
          Ned Murphy Adobe Community Professional & MVP

          While I can't say I understand what drives the precedence of events, when you process a key down event, I reckon only the last keydown event holds as the one that gets passed to the function.  So what you probably need to do is use these events to establish other controls for the movement, keeping track of when each is active versus not.... here's one possible approach...

           

          var goDown:Boolean = false;
          var goUp:Boolean = false;
          var goLeft:Boolean = false;
          var goRight:Boolean = false;


          function hearKeyDown(yourEvent:KeyboardEvent):void{    
          if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = true; }   
          if (yourEvent.keyCode==Keyboard.LEFT){ goLeft = true; }      
          if (yourEvent.keyCode==Keyboard.UP){ goUp = true; } 
          if (yourEvent.keyCode==Keyboard.DOWN){ goDown = true; }
          }

          function hearKeyUp(yourEvent:KeyboardEvent):void{    
          if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = false; }   
          if (yourEvent.keyCode==Keyboard.LEFT){ goLeft = false; }      
          if (yourEvent.keyCode==Keyboard.UP){ goUp = false; } 
          if (yourEvent.keyCode==Keyboard.DOWN){ goDown = false; }     
          }

          function moveCube(evt:Event):void {
          if (goRight){ cube_mc.x+=5 };   
          if (goLeft){ cube_mc.x-=5 };  
          if (goUp){ cube_mc.y-=5 };  
          if (goDown){ cube_mc.y+=5 };
          }

          stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKeyDown);
          stage.addEventListener(KeyboardEvent.KEY_UP, hearKeyUp);
          stage.addEventListener(Event.ENTER_FRAME, moveCube);