1

Is there any proper way to create and display a timer in java using only java sdk and libraries from libGDX?

I want my timer to start from zero and to be displayed in a format like minutes : seconds.

I am currently using workarounds like adding the delta time in render method and casting totalTime / 60 to integer for minutes and totalTime - minutes * 60 for seconds.

Before this I have tried adding the delta to seconds and having an if in the render method which sets seconds to 0 and increases minutes by 1 when seconds >= 59.9f. Both methods are very inefficient and not very accurate. My timer skips seconds when it approaches the end of a minute or it shows 60 seconds and then increases minutes and resets seconds, something like 1:59, 1:60 and then 2:00.

I searched all over the documentations and internet and not have found a way to properly display time (not workarounds that have huge flaws). How can you do this in a professional manner?

I don't think AAA games do this stuff seriously... Has anyone even thought that people might need something like a countdown timer in java or in libGDX (one that works without displaying 1:60 at least)?

2 Answers 2

5

It's kind of hard to follow exactly what you tried, so I'm not sure where you were losing accuracy, but maybe it had to do with where you put the integer cast in your calculation.

Generally a clock shows complete seconds, so round total time down to an integer. Minutes and seconds should be recalculated each frame. They serve no purpose except to display a human-readable version of totalTime, so there's no need to keep them in member variables. totalTime should be left unrounded so it stays accurate.

//Member variable:
float totalTime = 5 * 60; //starting at 5 minutes

void render(){
    float deltaTime = Gdx.graphics.getDeltaTime(); //You might prefer getRawDeltaTime()

    totalTime -= deltaTime; //if counting down

    int minutes = ((int)totalTime) / 60;
    int seconds = ((int)totalTime) % 60;

    //...
}

Edit 6 years later:

To avoid rounding error from adding up a bunch of small floating point delta times, I would instead store the system clock start time and recalculate the elapsed time each frame, like this:

// Reset this to System.currentTimeMillis() when starting over.
private long startTime = System.currentTimeMillis();

void render(){
    long totalTime = (startTime - System.currentTimeMillis()) / 1000;

    int minutes = totalTime / 60;
    int seconds = totalTime % 60;

    //...
}
Sign up to request clarification or add additional context in comments.

2 Comments

I will try this too. Until now I was using seconds as float: seconds = totalTime - minutes*60;
That explains the rounding issues you had. Since we just want to display an integer, it's the simplest tool for the job.
0

For my NoThree game, I print the timer as follows:

        gameTime += delta;
        float minutes = (float)Math.floor(gameTime / 60.0f);
        float seconds = gameTime - minutes * 60.0f;
        labelTime.setText(String.format("%.0fm%.0fs", minutes, seconds));

I think it works pretty nicely.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.