当我做了简单的单个线程的游戏我实现了游戏逻辑在几乎相同的方式将越来越多地著名Space Invaders tutorial做如下图所示:单独的线程的Java游戏逻辑
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();
}
如何正确拨打我的游戏实例的逻辑()方法?
就是这样!我和前一阵子非常接近,但是在'Game'和'game'之间有一个'=',在那里你写了'private game game;'DOH! – ubiquibacon 2010-09-06 02:33:09