When I have made simple single thread games I implemented the game logic in much the same way that the increasingly famous Space Invaders tutorial does as shown below:
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.
}
Now I want to try and run my logic on a separate thread but I am having problems. My game logic is in a separate class file which looks like this:
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.
}
}
... And my main method looks like this:
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();
}
How do I properly call the logic() method on the instance of my game?