【发布时间】:2010-09-06 02:20:56
【问题描述】:
当我制作简单的单线程游戏时,我以与越来越著名的Space Invaders tutorial 类似的方式实现游戏逻辑,如下所示:
public static void main(String[] args) { //This is the COMPLETE main method
Game game = new Game(); // Creates a new game.
game.gameLogic(); // Starts running game logic.
}
现在我想尝试在单独的线程上运行我的逻辑,但我遇到了问题。我的游戏逻辑在一个单独的类文件中,如下所示:
public class AddLogic implements Runnable {
public void logic(){
//game logic goes here and repaint() is called at end
}
public void paintComponent(Graphics g){
//paints stuff
}
public void run(){
game.logic(); //I know this isn't right, but I cannot figure out what to put here. Since run() must contain no arguments I don't know how to pass the instance of my game to it neatly.
}
}
...而我的主要方法是这样的:
public static void main(String[] args) { //This is the COMPLETE main method
Game game = new Game(); // Creates a new game.
Thread logic = new Thread(new AddLogic(), "logic"); //I don't think this is right either
logic.start();
}
如何在我的游戏实例上正确调用 logic() 方法?
【问题讨论】:
标签: java multithreading