为什么这段代码没有检测到对角线运动?

why doesn't this code detect diagonal motion?

"player" 是一个对象,当我按下箭头键时,它会向上、向下、向左和向右移动。

"player" 当我同时按下向上和向左键或任何其他键时,

"player" 不会沿对角线移动。

我正在使用 Adob​​e flash CS5 和动作脚本 3 (As3),你能告诉我解决方案是什么吗?

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;

            }

你可能把事情复杂化了。这可以通过以下方式完成。 KEY_DOWN 侦听器发生的情况是它将报告最后按下的键。所以你需要一个 ENTER_FRAME 监听器来移动对象。并且因为 ENTER_FRAME 将继续调用该函数,前提是您需要捕获释放键以将播放器的速度重置为 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;
}