简体   繁体   English

我的程序突然停止接收键盘输入

[英]My program suddenly stops receiving keyboard inputs

This is my first question so I'll just get straight to it. 这是我的第一个问题,所以我将直接解决。

I've been developing a little 2D platformer game in Java using the Eclipse Neon IDE. 我一直在使用Eclipse Neon IDE用Java开发一个小的2D平台游戏。 I've been able to overcome any issues I've encountered so far except for the one I'm about to tell you. 除了我要告诉你的问题以外,我已经能够克服到目前为止遇到的所有问题。

As game controls usually go, I've set up the WASD keys for movement and the SPACEBAR for jumping. 通常,随着游戏控制的进行,我已经设置了WASD键用于移动,并设置了SPACEBAR用于跳跃。 The character I control does respond to the key inputs so I know that this code works, but for some reason after the program has been running for about 3 minutes or so, the character stops receiving key inputs and I get this error: 我控制的字符确实响应了按键输入,因此我知道该代码可以工作,但是由于某些原因,程序运行了大约3分钟左右后,字符停止接收按键输入,并且出现此错误:

java.awt.AWTEventMulticaster.keyPressed(Unknown Source)

I've tried fiddling with the code to see if I could fix it myself, but I can't figure out what's causing the problem. 我尝试摆弄代码,看看自己是否可以修复它,但我不知道是什么引起了问题。

Here's the relevant section of code from my Player class. 这是Player类中代码的相关部分。 You probably don't need to pay attention to any of the variables in the keyPressed() and keyReleased() methods, but just the methods themselves: 您可能不需要注意keyPressed()和keyReleased()方法中的任何变量,而只需注意方法本身:

    public void keyPressed(KeyEvent keyID) { //This method checks to see which keys the user has pressed
    int key = keyID.getKeyCode();
    if (debug == false && key == KeyEvent.VK_F1) {
        debug = true;
    }
    else if (debug == true && key == KeyEvent.VK_F2) {
        debug = false;
    }

    if (death == false) {
        if (key == KeyEvent.VK_SPACE && jumping == false) {
            //Player.key = 2;
            velY = ySpeed;
            jumping = true;
            Sound.playSound("audio/playerJump.wav");
        }
        else if (key == KeyEvent.VK_D && rightIntersect == true) {
            velX = 0;
            lastKeyPressed = 'D';
            Player.key = 1;
        }
        else if (key == KeyEvent.VK_D && rightIntersect == false) {
            velX = xSpeed;
            lastKeyPressed = 'D';
            Player.key = 1;
        }
        if (key == KeyEvent.VK_A) {
            velX = -xSpeed;
            lastKeyPressed = 'A';
            Player.key = -1;
        }
    }

    else if (key == KeyEvent.VK_R && death == true) {
        for (int i = 0; i < GameFrame.getPlayerList().size(); i++) {
            Player p = GameFrame.getPlayerList().get(i);

            GameFrame.removePlayer(p);
            GameFrame.addPlayer(new Player(GameFrame.xPosStart, GameFrame.yPosStart));
            death = false;
            GameFrame.level = 1;
            GameFrame.mainTimer.setInitialDelay(0);
            GameFrame.mainTimer.start();
        }
    }
}

public void keyReleased(KeyEvent keyID) { //This method checks to see which keys the user has released
    int key = keyID.getKeyCode();

    if (key == KeyEvent.VK_A || key == KeyEvent.VK_D) {
        Player.key = 0;
        velX = 0;
    }
}

And here's the code for my KeyAdapt class that the Player class references: 这是Player类引用的KeyAdapt类的代码:

public class KeyAdapt extends KeyAdapter {
Player p;

public KeyAdapt (Player player) {
    p = player;
}   
public void keyPressed (KeyEvent keyID) {
    p.keyPressed(keyID);
}
public void keyReleased (KeyEvent keyID) {
    p.keyReleased(keyID);
}

Again, this code does work. 同样,此代码确实起作用。 It just suddenly stops working after the program has been running for a certain amount of time. 在程序运行了一定时间后,它突然突然停止工作。 Does anyone know what might be causing this issue? 有谁知道是什么原因导致此问题?

(If more information is needed, I can provide the entire code for the Player class). (如果需要更多信息,我可以提供Player类的完整代码)。

Does anyone know what might be causing this issue? 有谁知道是什么原因导致此问题?

It is not possible to say given the code you have shown us. 给定您显示给我们的代码,这不可能说。

I suggest that you add some debugging code. 我建议您添加一些调试代码。 For example, add something to keyPressed that logs the event received, and the state of variables such as death , jumping etcetera. 例如,在keyPressed中添加一些内容,以记录接收到的事件以及变量的状态,如deathjumping等。


You say: 你说:

I get this error: 我收到此错误:

  java.awt.AWTEventMulticaster.keyPressed(Unknown Source) 

That looks like part of a line from a stacktrace, not an error message. 这看起来像是来自堆栈跟踪的一行的一部分,而不是错误消息。 If this is a stacktrace, you need to show us the entire trace including the exception at the beginning. 如果这是一个堆栈跟踪,则需要向我们显示整个跟踪, 包括开头的异常。


Finally, you should not do this: 最后,您不应该这样做:

  if (death == false) {

The correct way to write that is: 正确的写法是:

  if (!death) {

It is more concise, but more importantly it is less fragile. 它更简洁,但更重要的是它不那么脆弱。 Imagine if you had mistyped your version as: 想象一下,如果您将版本错误输入为:

  if (death = false) {

One character difference. 一个字符的差异。 Easy to overlook. 容易忽视。 And it completely changes the meaning of the statement. 它完全改变了语句的含义。 It is a bad idea to use == and != to test boolean variables. 使用==!=测试boolean变量是一个坏主意

None of code that you showed us breaks because of this, but this bad habit could be used elsewhere in your code, and that might be the source of your problem. 您显示给我们的代码都不会因此而中断,但是这种不良习惯可以在代码的其他地方使用,这可能是问题的根源。

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

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