How to track time in Libgdx(android)

The Screen’s render(float delta) function gives you the amount of time that has passed by since the last render(..) call, in miliseconds, as float.

Therefore, a timer would just be the following:

public class GameScreen implements Screen
{
    private float bonusTimer;

    public GameScreen()
    {
        bonusTimer = 70f;
    }

    @Override
    public void render(float delta)
    {
        for(float iter = 0; iter < delta; iter += 0.125f)
        {
            float iterNext = iter + 0.125f;
            float currentDelta;
            if(iterNext > delta)
            {
                currentDelta = delta - iter;
            }
            else
            {
                currentDelta = iterNext - iter;
            }

            bonusTimer -= currentDelta;
            if(bonusTimer <= 0)
            {
                bonus();
                bonusTimer += 70f;
            }
            ...
        }
    }
    ...
}

What you would want to do is set an instance of this GameScreen to be the active Screen in the descendant of the Game, your core class. Look here for more info: https://github.com/libgdx/libgdx/wiki/Extending-the-simple-game

Adjust the logic as needed (for example, having your own GameModel that resets itself upon the start of the game).

Leave a Comment