Game loop
Every game runs in a loop called the game loop. The game loop is what makes your game update continuously and respond to input, animations, physics, and rendering.
In LibGDX (and the Saxion Game App), the game loop is hidden from you, but it still runs many times per second — usually 60 frames per second (FPS). The render(float delta) method is executed every loop. This is where you place the logic for your game.
Render method
When you implement the render(float delta) method, these elements should be in there:
1. Process Input
This is where you detect keyboard, mouse, or touch input.
Example: Check if a player pressed the jump button.
2. Update Game Logic
Here you move characters, check for collisions, update scores. Make sure that you use the delta parameter here to make the movement smooth. (See the segment below.)
3. Render the Game
Here you draw on the screen. You can draw shapes, textures (images) and text.
Before you start drawing, you will have to clear the previous frame first with the method GameApp.clearScreen(...).
Then draw shapes and textures using GameApp.startShapeRendering(), GameApp.endShapeRendering(), GameApp.startSpriteRendering(), GameApp.endSpriteRendering().
More information can be found in the Drawing shapes, Drawing textures and Text and fonts section.
4. Repeat
This is something that happens automatically. The render() method will be called 60 times per second (depending on what FPS you define in your setup).
💡 Tip: When writing your render method you only need to think about the current situation in your game and how that situation needs to be displayed.
Delta parameter
The delta parameter represents the amount of time (in seconds) that has passed since the last frame was rendered. Because different computers and devices can render frames at different speeds, the delta parameter is essential for making your game frame-rate independent.
Why is delta important?
Without delta, movement and animations would depend directly on how fast frames are drawn. For example:
- On a slower machine running 30 FPS, it moves 5 × 30 = 150 pixels per second.
- On a fast machine running 100 FPS, the object moves 5 × 100 = 600 pixels per second.
This creates inconsistent movement across devices. By using delta, you scale movement according to the time elapsed, not the frame count:
Now, no matter the frame rate:
- At 30 FPS, delta ≈ 1/30 ≈ 0.0333, so x moves 200 × 0.0333 ≈ 6.66 pixels per frame.
- At 60 FPS, delta ≈ 1/60 ≈ 0.0167, so x moves 200 × 0.0167 ≈ 3.34 pixels per frame.
The total movement per second remains approximately 200 pixels, making your game consistent.
The delta parameter can also be used to create timers, see the basic timers article.
Getting delta time outside the render method
Sometimes you need the delta variable from the render method, but it hasn't been passed to your own method. You can always get this value by retrieving it from the GameApp.