I've seen some tutorials on programming a good game loop, but none mention where (in the game code) I should call it.

My guess is I should call it from the main() method. Am I right? Is there another way?

Second question:

I'm using Java, and my game loop is a method in the same class as main(). When I tried to call it, the compiler did not recognize the gameLoop() method, because it wasn't static. So I changed it to static, as well as render() and update().

But it just doesn't make sense to me. What should I do in this case?


2 Answers 2


You can not have a non-static method in Main, because there is no instance of the Main class (unless you create one).

The game-loop can be placed wherever you feel is a good place for it. There is no "right" or "wrong" way to do this. Only the way which is right or wrong for you and your current project. However, there are best practices which happen to be right more often than others.

A common technique is to put the game loop method into an own class named Game or similar and create a new Game in the main-method. An advantage of this architecture is that you can have multiple Games which exist independent of each other.

You might also consider moving your update-method into a GameState class and your render-method into a Renderer class and pass the gamestate to it (renderer.render(gameState);). This allows you to reuse both components. You could reuse your graphic engine for an entirely different game by passing a GameState object with a different internal logic to it or you could replace your renderer with a different graphic engine. This would, for example, allow you to switch from a 2d engine to a 3d engine without having to change anything at the implementation of your game mechanics. You could also have different renderers for different views of the game state. A minimap, for example, could also be a different renderer. It also visualizes the current gamestate, just in a different way.

  • \$\begingroup\$ Thanks for the answer! But when you say GameState, are you talking about "menu" vs "game" vs "gameover" or something else? \$\endgroup\$
    – Marlon
    Jun 19, 2014 at 12:30
  • 1
    \$\begingroup\$ @Marlon No, with "GameState" I mean the current state of the game world. What objects you have in the game and what they are currently doing. \$\endgroup\$
    – Philipp
    Jun 19, 2014 at 12:32

How think about reading example of another game engine's? Such as cocos2dx, libgdx. You can get some ideas from it.

  • 3
    \$\begingroup\$ Your answer would be more helpful when you would explain how these engines solve the issue described and what the advantages and disadvantages of these solutions are. \$\endgroup\$
    – Philipp
    Jun 19, 2014 at 12:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .