简体   繁体   English

在2D canvas JavaScript游戏中对角移动

[英]Moving diagonally in 2d canvas JavaScript game

I've scoured the web for this answer. 我已经在网上搜寻了这个答案。 If it is buried in a StackOverflow answer somewhere I apologize. 如果它埋在StackOverflow中,请向我表示歉意。

I am working on a 2d canvas JavaScript game. 我正在开发2D canvas JavaScript游戏。

I am handling arrow key input with onkeydown and onkeyup events. 我正在处理onkeydown和onkeyup事件的箭头键输入。
I store this input in an object called Keys. 我将此输入存储在称为“键”的对象中。

var Keys = {
    up: false,
    down: false,
    left: false,
    right: false
};

This is what my event handlers look like: 这是我的事件处理程序的外观:

window.onkeydown = function(e){
     var kc = e.keyCode;
     e.preventDefault();

     if(kc === 37) Keys.left = true;
     else if(kc === 38) Keys.up = true;
     else if(kc === 39) Keys.right = true;
     else if(kc === 40) Keys.down = true;

     move();
 };

window.onkeyup = function(e){
     var kc = e.keyCode;
     e.preventDefault();

     if(kc === 37) Keys.left = false;
     else if(kc === 38) Keys.up = false;
     else if(kc === 39) Keys.right = false;
     else if(kc === 40) Keys.down = false;
};

Then each time the keydown event occurs, I call my move() function: 然后每次keydown事件发生时,我都会调用我的move()函数:

var move = function(){

    if(Keys.up){
        hero.y -= 10;
    }

    else if(Keys.down){
        hero.y += 10;
    }

    if(Keys.left){
        hero.x -= 10;
    }

    else if(Keys.right){
        hero.x += 10;
    }

    main();
}

The move() function calls my main() function which just draws the map again. move()函数调用我的main()函数,该函数再次绘制了地图。 I'm trying to avoid looping the game, and instead update the map each time the player moves. 我试图避免循环游戏,而是在每次玩家移动时更新地图。

So my problem arises when I try to move diagonally. 因此,当我尝试对角移动时会出现问题。 I am able to do it, however once I release the second key pressed, my character stops. 我可以做到,但是一旦释放第二个按键,角色就会停止。

For example: Right key pressed and then up key pressed, character moves northeast. 例如:按下右键,然后按下向上键,角色向东北移动。 Up key released, player stops. 释放向上键,播放器停止。

However, if I release the right key, the character continues moving up. 但是,如果我松开右键,角色将继续向上移动。

Another glitch is when I hold both left and right, the character will move left, but I want it to stop moving. 另一个故障是当我同时按住左右键时,角色将向左移动,但我希望它停止移动。

You mention that you want the player to stop moving when you press the left and right keys simultaneously. 您提到您希望播放器在同时按左右键时停止移动。 It appears you should be able to easily do this by replacing the else if statements in move with if statements, resulting in a move function similar to: 看样子,你应该能够很容易地做到这一点通过更换else if声明中moveif语句,导致move类似的功能:

var move = function(){

    if(Keys.up){
        hero.y -= 10;
    }

    if(Keys.down){
        hero.y += 10;
    }

    if(Keys.left){
        hero.x -= 10;
    }

    if(Keys.right){
        hero.x += 10;
    }

    main();
}

Quickly sketched an example, jsfiddle: http://jsfiddle.net/ofnp4vj4/ 快速勾勒出一个示例jsfiddle: http : //jsfiddle.net/ofnp4vj4/

HTML: <div id="log"></div> HTML: <div id="log"></div>

JS: JS:

var Keys = {
    up: false,
    down: false,
    left: false,
    right: false
};

var hero = {
    x: 0,
  y: 0
};

var log = document.getElementById("log");

window.onkeydown = function(e){
     var kc = e.keyCode;
     e.preventDefault();

     if(kc === 37) Keys.left = true;
     if(kc === 38) Keys.up = true;
     if(kc === 39) Keys.right = true;
     if(kc === 40) Keys.down = true;
 };

window.onkeyup = function(e){
     var kc = e.keyCode;
     e.preventDefault();

     if(kc === 37) Keys.left = false;
     if(kc === 38) Keys.up = false;
     if(kc === 39) Keys.right = false;
     if(kc === 40) Keys.down = false;
};



function main() {
  /* body */

    move();

  log.innerHTML = "x: "+hero.x+", y: "+hero.y;
};

function move(){

    if(Keys.up){
        hero.y -= 10;
    }

    if(Keys.down){
        hero.y += 10;
    }

    if(Keys.left) {
        hero.x -= 10;
    }

    if(Keys.right){
        hero.x += 10;
    }
}

setInterval(main, 100);

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

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