Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx how to calculate elapsed time

Tags:

java

timer

libgdx

I was trying to calculate elapsed time in libgdx by adding in the render method the value of delta to my float value which is the time i measure since play state starts. The bug or problem, how you want to call it, is that by my calculations, the time displayed doing this is two times the real time. I tried to divide by 2 and I was pretty close to the real time, which means that by adding delta every render call I don't get real time in seconds. Why is this happening?

private float time=0;
public void render () {
    time +=Gdx.graphics.getDeltaTime();
}

the above doesn't get the real time and my question is why? EDIT: i am using a screen but it doesn't matter i tried both Gdx.graphics.getDeltaTime() and delta argument from render method.

like image 964
Andrew Avatar asked May 29 '14 12:05

Andrew


1 Answers

You can use wrapper TimeUtils.

Get current time:

long startTime = TimeUtils.millis();

Get time elapsed since startTime:

long elapsedTime = TimeUtils.timeSinceMillis(startTime);
like image 146
Tomasz Posłuszny Avatar answered Nov 10 '22 00:11

Tomasz Posłuszny