启动线程时未调用Run。JAVA
所以,我开始为一个游戏编写代码,这次我想为launcher和游戏使用单独的类。 所以基本上它是一个单线程的游戏,线程是在启动器中创建和启动的,并且游戏类是使用run方法“Runnable”的。 问题是,即使我启动线程,run方法也不会被调用 发射器:启动线程时未调用Run。JAVA,java,Java,所以,我开始为一个游戏编写代码,这次我想为launcher和游戏使用单独的类。 所以基本上它是一个单线程的游戏,线程是在启动器中创建和启动的,并且游戏类是使用run方法“Runnable”的。 问题是,即使我启动线程,run方法也不会被调用 发射器: public class Launcher { private Thread thread; private static Game game; public static void main(String[] args) { Lau
public class Launcher {
private Thread thread;
private static Game game;
public static void main(String[] args) {
Launcher obj = new Launcher();
game = new Game();
obj.start();
}
public synchronized void start() {
if(game.running) return;
game.running = true;
thread = new Thread(game);
thread.start();
}
public synchronized void stop() {
if(!game.running) return;
game.running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
游戏类别:
public class Game extends JFrame implements Runnable{
public boolean running = false;
private static int WIDTH = 1280 , HEIGHT = 720;
private Screen screen;
public Game() {
this.setTitle("Game");
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.running = true;
this.setVisible(true);
init();
}
public void init() {
screen = new Screen(WIDTH, HEIGHT);
this.add(screen);
screen.init();
this.pack();
}
public void render() {
Screen.clearScreen();
Screen.update();
}
public void update() {
}
@Override
public void run() {
System.out.println("called"); //Checking if it was called, and it is not being called
long lastTime = System.nanoTime();
long now;
int fps = 60;
double timePerTick = 1000000000/fps;
double delta = 0;
while(running) {
now = System.nanoTime();
delta += (now - lastTime) / timePerTick;
lastTime = now;
if(delta >= 1){
update();
delta--;
}
render();
}
}
}
创建
newgame()
时,设置running=true
。稍后,在启动器中检查if(game.running)return,该值为true,并在之前返回
无关的:
为了支持封装,不要像在运行时那样公开公共字段。只需公开一些可以告诉您这些信息的方法,如:
public class Game{
private boolean running;
...
public boolean isRunning(){
return running;
}
}
创建
newgame()
时,设置running=true
。稍后,在启动器中检查if(game.running)return,该值为true,并在之前返回
无关的:
为了支持封装,不要像在运行时那样公开公共字段。只需公开一些可以告诉您这些信息的方法,如:
public class Game{
private boolean running;
...
public boolean isRunning(){
return running;
}
}
在这个构造函数中
public Game() {
this.setTitle("Game");
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.running = true;
this.setVisible(true);
init();
}
您必须设置this.running=false代码>以启动游戏线程。在此构造函数中
public Game() {
this.setTitle("Game");
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.running = true;
this.setVisible(true);
init();
}
您必须设置this.running=false
以启动游戏线程。创建游戏时,将running
设置为true:
public Game() {
//...
this.running = true;
调用start时,检查它是否已经运行,如果已经运行,则提前返回:
public synchronized void start() {
if(game.running) return;
//...
}
因此,在执行任何操作之前,您总是先退出此方法。创建游戏时,将running
设置为true:
public Game() {
//...
this.running = true;
调用start时,检查它是否已经运行,如果已经运行,则提前返回:
public synchronized void start() {
if(game.running) return;
//...
}
因此,在执行任何操作之前,您总是要退出此方法。这将始终适用:如果(game.running)返回代码>,因为游戏。运行是正确的(来自游戏的构造函数)。因此,您的线程从未启动。我还建议您阅读教程。执行Swing操作(如创建和显示JFrame
)应该在EDT(=事件调度线程)上进行,而不仅仅是在任何线程上。这将始终适用于:如果(游戏运行)返回代码>,因为游戏。运行是正确的(来自游戏的构造函数)。因此,您的线程从未启动。我还建议您阅读教程。执行Swing操作(如创建和显示JFrame
)应该在EDT(=事件调度线程)上进行,而不仅仅是在任何线程上。是的,谢谢!我才意识到这有多傻。“是的,谢谢!我才意识到这有多傻。”你是说假的。是的。谢谢:D。你是说假的。谢谢:D。