(對不起,我無法解決這個問題,我需要幫助?。┪艺趧摻ㄒ粋€自定義游戲引擎,并遇到了一個問題,即在游戲運行時 - 游戲停止接受輸入我檢查過,程序似乎繼續在后臺運行。它似乎也不會因不同的機器而有所不同(我的主要設備是 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 的了解不夠了解是否情況就是如此,重新定位備份中的代碼會導致程序崩潰。任何幫助將不勝感激。
添加回答
舉報
0/150
提交
取消