Skip to content

Timers (advanced)

⚠️ Advanced Topic

This section is for students who already understand how to use simple timers inside the render(float delta) method (see the Timers (basics) section first).

⚠️ Experimental feature

This functionality has been added last-minute and it therefore not tested extensively. Use it at your own risk. Some behaviors or methods may change in future updates.

In the basic examples, you created your own timer variables, added delta, and reset them when they reached a certain value. That works well for a few timers, but as your games grow, it can become messy to manage multiple timers this way.

To make it easier, our game engine includes a Timer System. It lets you create timers by name, update them all at once, and check if they went off (without having to write the same logic every time).

With this system, you can:

  • Easily create many timers without extra variables.
  • Keep your code clean and organized.
  • Pause, resume, and reset timers whenever you need.
  • Set timers to repeat or run only once.
  • Check how much time is left!

Creating Timers

There are three ways to create a timer, depending on what you want it to do.

One-Time Timer

This timer goes off once, then stops.

GameAppTimers.addTimer("powerup", 5f, false);
  • powerup = name of the timer
  • 5f = duration in seconds before the timer goes off
  • false means: execute the timer only once

Looping Timer

By changing the last value to true, the timer will go off every 2 seconds:

GameAppTimers.addTimer("enemy", 2f, true);

Limited Loops

You can also choose exactly how many times a timer should go off.

GameAppTimers.addTimer("shoot", 1f, 3);

This means: The timer “shoot” will go off every 1 second, three times total, then stop.

Updating Timers

You should update your timers every frame inside the render(float delta) method. This allows all timers to count down using the delta time.

GameAppTimers.updateTimers();

Checking a Timer

To see if your timer went off, use:

if (GameAppTimers.wentOff("enemy")) {
    // Do something
}

If the timer loops, it will restart automatically.

Pausing and Resuming

You can stop a timer temporarily (for example, when your game is paused):

GameAppTimers.pauseTimer("enemy");

Then continue later (when resumed):

GameAppTimers.resumeTimer("enemy");

Resetting a Timer

If you want a timer to start over right away:

GameAppTimers.resetTimer("enemy");

Seeing Remaining Time

You can ask how much time is left before it goes off:

float remaining = GameAppTimers.getRemaining("enemy");
System.out.println("Time left: " + remaining);

Is completely finished

When you are using limited loops, you might want to find out when the timer has completely finished. This can be done as follows:

if (GameAppTimers.isTimerFinished("shoot")) {
    System.out.println("Shoot timer done!");
}

Changing the Duration

You can change how long a timer takes, even while it’s running:

GameAppTimers.setDuration("enemy", 4f);

This will make the timer go off every 4 seconds instead.

Notes

  • Always call GameAppTimers.updateTimers() inside render().
  • Give each timer a unique name.
  • Looping timers restart automatically.
  • Non-looping timers stop after going off once.
  • Limited timers stop after their set number of loops.

Example

Example

This example shows how to create a repeating timer that goes off every 2 seconds.

public class ExampleScreen extends ScalableGameScreen {

    ...

    @Override
    public void show() {
        // Create a timer named "spawn"
        // It goes off every 2 seconds and repeats forever
        GameAppTimers.addTimer("spawn", 2f, true);
    }

    @Override
    public void render(float delta) {
        super.render(delta);
        GameApp.clearScreen();

        // Update all timers once per frame
        GameAppTimers.updateTimers();

        // Check if the "spawn" timer went off
        if (GameAppTimers.wentOff("spawn")) {
            System.out.println("Enemy spawned!");
        }
    }
}

🕒 This will print “Enemy spawned!” every 2 seconds.