Reputation: 148
I must be making some silly mistake in my code, In my head this should work but the timer works a bit too fast ( I want to get time left in seconds).
My code:
timeElapsed = 0;
timeLeft = 60;
//delta = time it took to get through one frame (60 fps).
timeElapsed += delta/getFramesPerSecond(); //FPS = 60f
timeLeft -= timeElapsed; //timeLeft starts at 60 (seconds)
timeDisplay = "Time left: " + timeLeft;
I have checked that FPS is always 60, what am I missing here?
Delta sample prints:
0.016969847
0.017038532
0.017123796
0.017026689
0.016969848
0.017059453
0.01697774
0.016987609
0.017073665
0.017035767
0.01708432
Upvotes: 1
Views: 717
Reputation: 25972
timeElapsed+timeLeft
should be a constant (thus both should change by the same amount in opposite directions), however, you are repeatedly reducing timeLeft
by timeElapsed
In a demonstrative example with steps 1 in timeElapsed
, your code gives
timeElapsed timeLeft
0 60
1 59
2 57
3 54
4 50
5 45
6 39
7 32
Change the code to
timeDelta = delta/getFramesPerSecond();
timeElapsed += timeDelta;
timeLeft -= timeDelta;
timeDisplay = "Time left: " + timeLeft;
Upvotes: 1