이 문제를 해결하기 위해 여러 가지 방법을 시도했지만이 문제를 해결할 수있는 방법을 찾지 못했습니다. 게임을 멈추더라도 게임 쓰레드가 멈추지 않습니다!게임 스레드가 멈추지 않습니다
(메인 클래스)
public class Game extends JFrame implements Runnable {
private static final long serialVersionUID = 4662621901369762109L;
public static final Rectangle windowSize = new Rectangle(800, 600);
public static final int fps = 60;
private static Game instance;
private static Thread gameThread;
public static final PaintCanvas canvas = new PaintCanvas();
public Game() {
this.setSize(windowSize.getWidth(), windowSize.getHeight());
this.setTitle("Test");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
this.add(canvas);
Spawner mainSpawner = new Spawner();
mainSpawner.setPosition(new Point(windowSize.getWidth()/2 - 30, windowSize.getHeight()/2 - 30));
}
public static void main(String[] args) {
instance = new Game();
gameThread = new Thread(instance);
gameThread.start();
}
public void run() {
while (true) {
update();
try {
// 1000 divided by fps (for 60 fps, it is 16.666 repeating)
Thread.sleep(1000/fps);
}
catch (InterruptedException e) {
break;
}
}
}
private static void update() {
for (GameObject g : new ArrayList<GameObject>(GameObjectManager.getGameObjects())) {
g.update();
}
canvas.repaint();
}
public static Game getInstance() {
return instance;
}
}
내가 스레드에서 정말 나쁜 나는 그렇게 도와주세요! 사물의
또한 'InterruptedException'이 발생한 경우에만 종료되는 'while (true)'무한 루프가있는 것처럼 보입니다. 발생하지 않으면 무한 루프가됩니다. – SnakeDoc
GUI 코드는 관련성이 없으므로 멈추려는 시도조차 보이지 않습니다. while 루프에 사용할 부울을 만들고, 중지 시키려면 false로 설정하십시오. 또는 다른 스레드에서 스레드를 인터럽트 할 수 있습니다. –
플레이어가 게임을 종료 할 때 멈추고 싶습니다. While while (gameThread! = null) while 루프를 변경해 보았지만 여전히 작동하지 않습니다. – MCMastery