Java - KeyListener 多个按钮按下 [英] Java - KeyListener multiple button presses

查看:37
本文介绍了Java - KeyListener 多个按钮按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我能以正确的方式描述我的问题.我现在正在编写一个简单的 Twin-Stick Shooter,所以我实现了一个实现 KeyListener 的 KeyInputHandler 类.但是当玩家同时按下 2 个按钮时,我遇到了问题.在玩家真正移动之前总是需要一秒钟.据我所知,这是因为密钥重复.我可能错了.我用谷歌搜索了一下,在这里找到了一个可能的解决方案:https://gamedev.stackexchange.com/questions/51623/smoother-controls?rq=1人们建议不要在按键时改变玩家的 x 和 y 速度,而是在玩家按下某个按钮时标记方向并以不同的方法处理移动.所以我创建了布尔变量并在按下按钮时激活它们:

I hope I can describe my problem in the right way. I am programming a simple Twin-Stick Shooter at the moment so I implemented a KeyInputHandler calss that implements KeyListener. But when the player pressed 2 buttons at the same time I ran into a problem. It took always a second before the player actually moves. As far as I know that is because of the key repeat. I might be wrong. I googled a bit and found a possible solution here: https://gamedev.stackexchange.com/questions/51623/smoother-controls?rq=1 The people suggested to not change the x- and y-velocity of the player on key press but instead flag the direction when the player presses a certain button and handle the movement in a different method. So I created the boolean variables and activated them when the button is pressed:

public class KeyInputHandler implements KeyListener{

private Game game;
// checks if A key is down
public boolean left_key_down = false;
// checks if D key is down
public boolean right_key_down = false;
// checks if W key is down
public boolean up_key_down = false;
// checks if S key is down
public boolean down_key_down = false;

public KeyInputHandler(Game game) {
    this.game = game;
}

public void keyPressed(KeyEvent e) {
    int keys = e.getKeyCode();

    if (keys == KeyEvent.VK_D) {
        right_key_down = true;
    } else if (keys == KeyEvent.VK_A) {
        left_key_down = true;
    } else if (keys == KeyEvent.VK_W) {
        up_key_down = true;
    } else if (keys == KeyEvent.VK_S) {
        down_key_down = true;
    }
}

public void keyReleased(KeyEvent e) {
    int keys = e.getKeyCode();

    if (keys == KeyEvent.VK_D) {
        right_key_down = false;
    } else if (keys == KeyEvent.VK_A) {
        left_key_down = false;
    } else if (keys == KeyEvent.VK_W) {
        up_key_down = false;
    } else if (keys == KeyEvent.VK_S) {
        down_key_down = false;
    }
}

public void keyTyped(KeyEvent e) {

}

public void tick() {

    if (left_key_down) {
        game.p.setVelX(-1);
    } else if (right_key_down) {
        game.p.setVelX(1);
    } else if (up_key_down) {
        game.p.setVelY(-1);
    } else if (down_key_down) {
        game.p.setVelY(1);
    } else {
        game.p.setVelY(0);
        game.p.setVelX(0);
    }
}
}    

(在主游戏循环中调用tick()方法)

(The tick() method is called in the main game loop)

现在的问题是不再有延迟(是的),但是当我同时按下 2 个按钮时,事情变得很奇怪.示例:

The problem is now that there is no delay anymore (yay), but the things get strange when I press 2 buttons together. Example:

  1. 我按下左按钮
  2. 我按下向下按钮
  3. 我松开了向下按钮 -> 对象仍然向左移动
  4. 我向上按 -> 对象向左移动

但是当我按住 Right 然后又按住 Down 时,它只是向下移动.

But when I hold Right and then als hold Down it is just moving down.

我还检查了布尔变量,它们正在正确刷新.这有点奇怪,我不确定如何处理这个问题以及究竟是什么导致了这种行为.我假设它与检查cooleans的顺序有关.也许有人可以帮助我解决这个问题.我将不胜感激.:)

I also checked the boolean variables and they are getting refreshed correctly. This is kinda weird and I am not really sure how to handle this and what exactly is causing this behaviour. I have the assumption that it has something to do with the order in which the cooleans are checked. Maybe someon can help me on this matter. I would greatly appreciate it. :)

推荐答案

同时按下多个键的情况变得奇怪的问题是,您在检查布尔值时使用了 if else.将 tick() 中的所有布尔检查分开以拥有单独的 ifs.如果任何一个按钮被持续按下,代码永远不会进入最后一个 else 块,在那里 VelX 和 VelY 被重置为零.

The problem why things are getting strange in multiple key presses together is that you are using if else's in checking the booleans. Separete all the boolean checks in tick() to own individual ifs. If any one of the buttons are being pressed constantly, the code never goes to the last else-block where VelX and VelY are reset to zero.

将tick方法中的代码改成这样:

Change the code in tick method to something like this:

//Set 'velocity' to zero first
game.p.setVelY(0);
game.p.setVelX(0);

if (left_key_down) {
    game.p.setVelX(-1);
} 
if (right_key_down) {
    game.p.setVelX(1);
} 
if (up_key_down) {
    game.p.setVelY(-1);
} 
if (down_key_down) {
    game.p.setVelY(1);
} 

这篇关于Java - KeyListener 多个按钮按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆