(对不起,我无法解决这个问题,我需要帮助!)
我正在创建一个自定义游戏引擎,并遇到了一个问题,即在游戏运行时 - 游戏停止接受输入
我检查过,程序似乎继续在后台运行。它似乎也不会因不同的机器而有所不同(我的主要设备是 Mac Book Pro 2011)
import java.awt.event.*;
import java.io.IOException;
import java.awt.*;
import javax.swing.*;
public class Focus extends JFrame implements KeyListener {
private static final long serialVersionUID = 1L;
char currKey = '\0';
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {public void run() {new UIManager();}});
}
public Focus() throws IOException {
Container contentPane = getContentPane();
contentPane.add(new DrawCanvas());
addKeyListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setFocusable(true);
setVisible(true);
}
private class DrawCanvas extends JPanel {
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics pen) {
//Drawloop
if(currKey == 'k') {
//This is the code that randomly stops running
System.out.println("Yo");
}
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
@Override
public void keyTyped(KeyEvent e) {
currKey = e.getKeyChar();
}
@Override
public void keyPressed(KeyEvent e) {
currKey = '\0';
}
@Override
public void keyReleased(KeyEvent e) {
}
}
代码在我看来是正确的(但是,它总是如此)并且唯一可能的触发点是 AWT 在 Main 中实例化,在 UIManager 中运行并且移动代码驻留在播放器中,尽管我对 AWT 的了解不够了解是否情况就是如此,重新定位备份中的代码会导致程序崩溃。任何帮助将不胜感激。
互换的青春
相关分类