简体   繁体   English

HTML5画布游戏移动x,y

[英]Html5 canvas game moving x, y

I got my code to work, and you can control an element using the arrow keys. 我的代码可以工作了,您可以使用箭头键控制元素。 However, i only want my element to move up-down-left-right. 但是,我只希望我的元素上下左右移动。 So when i press UP, i should not be able to press RIGHT at the same time (strafing). 因此,当我按UP键时,我应该不能同时按RIGHT键。 But I'm not sure how to prevent this? 但是我不确定如何防止这种情况发生? Anyone that can put me in the right direction? 有人可以将我带往正确的方向吗?

How i check my movement, 我如何检查运动

// Tracking keys
var keysDown = {};

// Moving the player around on the map            
if(39 in keysDown)  { player.x+=gameSettings.speed; } // Move Right
if(37 in keysDown)  { player.x-=gameSettings.speed; } // Move Left 
if(38 in keysDown)  { player.y-=gameSettings.speed; } // Move Up
if(40 in keysDown)  { player.y+=gameSettings.speed; } // Move Down

addEventListener('keydown', function(e) {
   keysDown[e.keyCode] = true; 
}, false);

addEventListener('keyup', function(e) {
   delete keysDown[e.keyCode]; 
}, false);

I'm sure there is an easy solution, but I've been looking at this too long, so any pinpoints in the right direction would be great! 我敢肯定有一个简单的解决方案,但是我一直在研究这个问题,所以在正确方向上的任何精确定位都是不错的选择!

I like miqdadamirali's answer, but lets see if we can simplify it a bit. 我喜欢miqdadamirali的答案,但让我们看看是否可以简化一下。

Try this: 尝试这个:

// Tracking keys
var keysDown = {};

// Moving the player around on the map            
if(39 in keysDown)  { player.x+=gameSettings.speed; } // Move Right
if(37 in keysDown)  { player.x-=gameSettings.speed; } // Move Left 
if(38 in keysDown)  { player.y-=gameSettings.speed; } // Move Up
if(40 in keysDown)  { player.y+=gameSettings.speed; } // Move Down

addEventListener('keydown', function(e) {
   // we only want to set the key if it's empty
   if (Object.keys(keysDown).length === 0) {
       keysDown[e.keyCode] = true;
       // call the function to make the actual movement
   }
}, false);

addEventListener('keyup', function(e) {
   delete keysDown[e.keyCode]; 
}, false);

Two assumptions here: 这里有两个假设:

  1. The block of code that does the moving is called every time a keydown event is fired. 每当触发keydown事件时,都会调用进行移动的代码块。 I assume you are showing here for illustration purpose. 我假设您在这里显示是出于说明目的。
  2. Based on your description, there can only be one moving at a time, so we should only have one property in the keysDown object at any time. 根据您的描述,一次只能移动一个,因此我们在任何时候都应该在keysDown对象中只有一个属性。 You could have used a variable and set it to zero or 39 - 40. 您可以使用变量并将其设置为零或39-40。

EDIT: 编辑:

To illustrate the even more simplified version, here's the code. 为了说明更简化的版本,下面是代码。

// Tracking keys
var keyPressed = 0;

// Moving the player around on the map            
if(keyPressed === 39)  { player.x+=gameSettings.speed; } // Move Right
if(keyPressed === 37)  { player.x-=gameSettings.speed; } // Move Left 
if(keyPressed === 38)  { player.y-=gameSettings.speed; } // Move Up
if(keyPressed === 40)  { player.y+=gameSettings.speed; } // Move Down

addEventListener('keydown', function(e) {
   // we only want to set the key if keyPressed is zero
   if (keyPressed === 0) {
       keyPressed = e.keyCode;
       // call the function to make the actual movement
   }
}, false);

addEventListener('keyup', function(e) {
   keyPressed = 0;
}, false);

Before doing anything to the second key, check it is possible to move 在对第二把钥匙进行任何操作之前,请检查是否可以移动

Try This: 尝试这个:

// Tracking keys
var keysDown = {};
var canMove = true; // If it can move.

// Moving the player around on the map            
if(39 in keysDown)  { player.x+=gameSettings.speed; } // Move Right
if(37 in keysDown)  { player.x-=gameSettings.speed; } // Move Left 
if(38 in keysDown)  { player.y-=gameSettings.speed; } // Move Up
if(40 in keysDown)  { player.y+=gameSettings.speed; } // Move Down

addEventListener('keydown', function(e) {
   if (canMove) {
      keysDown[e.keyCode] = true; 
      canMove = false;
   }
}, false);

addEventListener('keyup', function(e) {
   if (keysDown[e.keyCode]) { 
        delete keysDown[e.keyCode]; 
        canMove = true;
   }
}, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM