Search code examples
actionscript-3flashadobemotion

why doesn't this code detect diagonal motion?


"player" is an object that moves up , down , left and right when I press on arrow keys.

"player" doesn't move diagonally when I press up and left keys together or any other couple of keys.

I'm using Adobe flash CS5 and action script 3 ( As3 ), Would you tell me what the solution is ?

stage.addEventListener(KeyboardEvent.KEY_DOWN, detectText);     
function detectText(myevent:KeyboardEvent):void {
         var up:Boolean = false;
         var down:Boolean = false;
         var left:Boolean = false;
         var right:Boolean = false;
         var speedOfplayer:Number = 5 ;
 if (myevent.keyCode==39){
    right = true ;


}

 if (myevent.keyCode==37){
    left = true ;
} 

 if (myevent.keyCode==38){
    up = true ;                     

} 

 if (myevent.keyCode==40){
    down = true ;
} 
            // if(right is true and left is not true)
            if( right && !left ) {
                player.x = player.x + speedOfplayer;
            }
            // if(up is true and down is not true)
            if( up && !down ) {
                player.y = player.y - speedOfplayer;        
            }
            // if(down is true and up is not true)
            if( down && !up ) {
                player.y = player.y + speedOfplayer;
            }
            // if(down is true and up is not true)
            if( left && !right ) {
                player.x = player.x - speedOfplayer;

            }


            // Move diagonally
            if( left && up && !right && !down ) {
                player.y = player.y - speedOfplayer;        
                player.x = player.x - speedOfplayer;

            }
            if( right && up && !left && !down ) {
                player.x = player.x + speedOfplayer;
                player.y = player.y - speedOfplayer;        

            }
            if( left && down && !right && !up ) {
                player.x = player.x - speedOfplayer;
                player.y = player.y - speedOfplayer;        
            }
            if( right && down && !left && !up ) {
                player.x = player.x + speedOfplayer;
                player.y = player.y + speedOfplayer;

            }

Solution

  • You may be overcomplicating things. This can be done in the following way. What is happening to the KEY_DOWN listener is that it will report the last key pressed. So you need a ENTER_FRAME listener to move the object. And because ENTER_FRAME will keep calling the function provided you need to capture if a key is released to reset the speed of the player to 0.

    stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
    stage.addEventListener(Event.ENTER_FRAME, onTick);
    
    var speedX:uint = 0;
    var speedY:uint = 0;
    
    function onKeyDown(event:KeyboardEvent):void {
         var key:uint= event.keyCode;
    
         if (key == Keyboard.UP) {
             speedY = -5;
         }
         if (key == Keyboard.DOWN) {
             speedY = 5;    
         }
         if (key == Keyboard.LEFT) {
             speedX = -5;
         }
         if (key == Keyboard.RIGHT) {
             speedX = 5;
         }
    }
    
    function onKeyUp(event:KeyboardEvent):void {
      var key:uint= event.keyCode;
      if (key == Keyboard.UP || key == Keyboard.DOWN) {
          speedY = 0;
      }
      if (key == Keyboard.LEFT || key == Keyboard.RIGHT) {
          speedX = 0;   
      }
    }
    
    function onTick(event:Event):void {
        player.x += speedX;
        player.y += speedY;
    }