I got a simple 2D game in Java. The chararcters on the screen are controlled using Key Bindings.
The game uses a class that extends JPanel, to display the graphics and contain most of the game logic. This class' name is Board
.
What type of game loop should I use to every 40 milliseconds the game and repaint() the Board
?
Option A:
Board
contains a thread that starts when the program starts. This thread contains a loop that once in 40 ms updates the game and repaints() Board.
Option B:
Board creates a Swing timer. This timer's action listener is Board itself. The actionPerformed() method runs every 40 ms and updates the game + repaints Board().
In other words, in a 2D game with Key Bindings, what's better for the game loop and why - Swing timer or a thread with a loop inside it?
Thanks