在输入被按下之前,键输入不起作用

时间:2014-01-24 00:39:31

标签: java input key

我正在制作一个Java游戏,每当我启动它时,没有任何键可以工作,直到我按下回车键。这是关键输入代码:

if(KeyInput.currentKey(KeyInput.W)) player.setUp();
if(KeyInput.currentKey(KeyInput.A)) player.setLeft();
if(KeyInput.currentKey(KeyInput.S)) player.setDown();
if(KeyInput.currentKey(KeyInput.D)) player.setRight();
if(KeyInput.currentKey(KeyInput.UP)) player.setDirection(0);
if(KeyInput.currentKey(KeyInput.RIGHT)) player.setDirection(1);
if(KeyInput.currentKey(KeyInput.DOWN)) player.setDirection(2);
if(KeyInput.currentKey(KeyInput.LEFT)) player.setDirection(3);
if(KeyInput.currentKey(KeyInput.ENTER)) player.setPunching();

我有一个名为KeyInput的类,方法currentKey只检查是否有按下的键,如果有,则检查参数中的键是否为当前键。这是代码:

public class KeyInput {

public static final int NUM_KEYS = 11;

public static boolean keyState[] = new boolean[NUM_KEYS];
public static boolean prevKeyState[] = new boolean[NUM_KEYS];

public static int W = 0;
public static int A = 1;
public static int S = 2;
public static int D = 3;
public static int ENTER = 4;
public static int UP = 5;
public static int LEFT = 6;
public static int DOWN = 7;
public static int RIGHT = 8;
public static int ESCAPE = 9;
public static int SHIFT = 10;

public static void setKey(int i, boolean b) {
    if(i == KeyEvent.VK_W) keyState[W] = b;
    else if(i == KeyEvent.VK_A) keyState[A] = b;
    else if(i == KeyEvent.VK_S) keyState[S] = b;
    else if(i == KeyEvent.VK_D) keyState[D] = b;
    else if(i == KeyEvent.VK_ENTER) keyState[ENTER] = b;
    else if(i == KeyEvent.VK_UP) keyState[UP] = b;
    else if(i == KeyEvent.VK_LEFT) keyState[LEFT] = b;
    else if(i == KeyEvent.VK_DOWN) keyState[DOWN] = b;
    else if(i == KeyEvent.VK_RIGHT) keyState[RIGHT] = b;
    else if(i == KeyEvent.VK_ESCAPE) keyState[ESCAPE] = b;
    else if(i == KeyEvent.VK_SHIFT) keyState[SHIFT] = b;
}

public static void update() {
    for(int i = 0; i < NUM_KEYS; i++) {
        prevKeyState[i] = keyState[i];
    }
}

public static boolean currentKey(int i) {
    return keyState[i];
}

}

在Main类中调用setKey方法:

public void keyTyped(KeyEvent key) {}
public void keyPressed(KeyEvent key) {
    KeyInput.setKey(key.getKeyCode(), true);
}
public void keyReleased(KeyEvent key) {
    KeyInput.setKey(key.getKeyCode(), false);
}

我不知道为什么会这样,我不知道你们是否能够在不了解游戏编程方式的情况下提供帮助。提前致谢! :)

1 个答案:

答案 0 :(得分:0)

imho您的代码中没有足够的信息来解决问题。

检查正在初始化KeyListener的点/对象。也许这是第一次与Enter-Key相关的行动?

编辑:您应该将keyListener添加到面板中。你不能像普通类一样处理Listener,它们是一种叫做Interface的特殊类,它就像普通类的扩展。将KeyListener接口添加到游戏面板,它将观察面板以进行与键盘相关的操作。

你可以在这里找到一个与游戏相关的例子: http://www.edu4java.com/en/game/game4.html

看起来应该是这样的:

JPanel gamePanel = new JPanel();
gamePanel.addKeyListener(new KeyListener(){

    @Override
    public void keyPressed(KeyEvent arg0) {
        if(arg0.getExtendedKeyCode() == KeyEvent.VK_ENTER) player.setPunching();
        //and so on
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {

    }

});