使用增量计算经过的时间

Calculating elapsed time using delta

我一定是在我的代码中犯了一些愚蠢的错误,在我看来这应该可行,但计时器运行得太快了(我想以秒为单位计算剩余时间)。

我的代码:

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;

我检查过 FPS 始终为 60,我在这里缺少什么?

Delta 样本打印:

0.016969847
0.017038532
0.017123796
0.017026689
0.016969848
0.017059453
0.01697774
0.016987609
0.017073665
0.017035767
0.01708432

timeElapsed+timeLeft 应该是一个常数(因此两者应该在相反的方向上以相同的量变化),但是,您重复将 timeLeft 减少 timeElapsed

timeElapsed 中第 1 步的演示示例中,您的代码给出了

timeElapsed   timeLeft
    0            60
    1            59
    2            57
    3            54
    4            50
    5            45
    6            39
    7            32

将代码更改为

timeDelta    = delta/getFramesPerSecond(); 
timeElapsed += timeDelta; 
timeLeft    -= timeDelta; 
timeDisplay  = "Time left: " + timeLeft;