I try to add time to my game that will increment every second. How to fix that?
I add my code below
float timer; timer += delta; if(timer<=delta+1000)//every one sec { time = time+1; timePoint.setSentence(""+time/100); timer = 0f; }
as your note, delta is Gdx.graphics.getDeltaTime(). ‘time’ is string. but looks like the time increment slowly. means that when time already run about 1,5 sec, ‘time’ still increase 1. I divide by 100 because if not it will increase more faster/sec,
i also use TimeUtils class from libgdx but it produced similar result.
Thanks before.
Advertisement
Answer
This should work. Note that time/100
results in 0 for 0-99 and 1 for 100-199. That’s probably not the effect you wanted.
float timer; timer += delta; if (timer >= 1) { time++; timePoint.setSentence(""+time); timer -= 1; }
The problem was that you set the timer back to 0. If it was 1.1f for example (because the last delta was 0.1f), then you basically lose 100ms. Don’t reset the timer to 0f, but decrease it by 1f instead.