0

"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;

            }
user2824371
  • 127
  • 9

1 Answers1

3

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;
}
Dimitris
  • 403
  • 4
  • 10
  • Thank you very much for your answer, I would like to ask you some questions, – user2824371 Mar 16 '15 at 12:55
  • I've made some edits in your code because: speedX and speedY variables are uInt which must have positive values only, and they are equals to negative five in IF statements. so, I converted them to :Number. "onTick" in the event listener is not " _on Tick" in the function. I've also declared the variable "Key" before the function "onKeyDown" because it has an error. then, after the function I wrote the following code: key = event.keyCode; – user2824371 Mar 16 '15 at 13:05
  • After my Editing, when I press "CTRL + Enter" to Test it. the "player" object moves diagonally down and right ( Without pressing any keys ) when I press arrow keys, it listens to instructions. but when I release all Keys, It moves again at the direction of release ( Up or down or right or lift ). Would you please give me a hand ? – user2824371 Mar 16 '15 at 13:09
  • There is a warning, I don't know if this error causes the issue mentioned above or not. The warning says: " Migration issue: the onKeyDown event handler is not triggered automatically by Flash player at run time in Action Script 3.0 you must first register this handler for the event using addEventListener('keyup' , callback_handler). Thanks in advance, – user2824371 Mar 16 '15 at 13:16
  • 1
    The auto movement of the player you mention in you third comment is caused because the variables speedX & speedY are equal to 5. I have updated the code so that the speed variables equal to 0. Now about the final comment, I'm not sure why there's a warning. try importing the class of the KeyboardEvent. import flash.events.KeyboardEvent – Dimitris Mar 17 '15 at 09:33