Java - 游戏开发:一次按下多个键的问题

时间:2014-02-15 00:50:26

标签: java keypress keylistener keyrelease

我是一个noob编码器,我正在尝试制作一个小型2D太空射击游戏,但我遇到了一个问题。

我在对角线移动时无法拍摄。这个问题也与我遇到的一些问题有关,例如:我开始举起,然后开始按住,然后向下释放但是向上键的keylistener不再工作,所以我不再移动。

以下是我的一些代码:

    // This method is called by the KeyInput instance created up above when a key is pressed
public void keyPressed(KeyEvent e)
{
    // Grabbing the key code that was pressed
    int keyCode = e.getKeyCode();

    // Accelerating the player based on key pushed
    if (keyCode == KeyEvent.VK_RIGHT) {
        player.setAccX(player.getAccTick());

    } else if (keyCode == KeyEvent.VK_LEFT) {
        player.setAccX(-player.getAccTick());

    } else if (keyCode == KeyEvent.VK_DOWN) {
        player.setAccY(player.getAccTick());

    } else if (keyCode == KeyEvent.VK_UP) {
        player.setAccY(-player.getAccTick());

    } else if (keyCode == KeyEvent.VK_SPACE) {
        bm.addBullet(new PlayerBullet(player.getX(), player.getY(), this));
    }
}

// This method is called by the KeyInput instance created up above when a key is released
public void keyReleased(KeyEvent e)
{
    // Grabs the key code of the key released
    int keyCode = e.getKeyCode();

    // Sets the accelerationg to 0 based on the key released
    if (keyCode == KeyEvent.VK_RIGHT) {
        player.setAccX(0);

    } else if (keyCode == KeyEvent.VK_LEFT) {
        player.setAccX(0);

    } else if (keyCode == KeyEvent.VK_DOWN) {
        player.setAccY(0);

    } else if (keyCode == KeyEvent.VK_UP) {
        player.setAccY(0);
    }
}

我希望能够同时保持多个方向以及同时射击子弹。我知道这不是我的键盘,因为我已经玩过这样的其他游戏,我可以按住几个按键然后继续拍摄。谢谢你的帮助!

:d

0 个答案:

没有答案