这只是我的代码的一部分,我希望很容易理解。我找到了一种“修复”它的方法,但我仍然不明白这一点:
我将浮动 countDownTime 设置为 2f。在 DisplayTime() 中,我认为 do-while 循环将从 2 倒数到 0,而是从 0 倒数到负数。我认为当 countDownTime 达到 0 时它会倒计时并停止,正如在 while(countDownTime >= 0) 中分配的那样,但它会继续超出此范围。感谢您提供任何反馈或帮助。
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && !hasStarted)
{
hasStarted = true;
StartGame();
}
DisplayTime();
}
void DisplayTime()
{
if (timerStart)
{
do
{
countDownTime -= Time.deltaTime;
} while (countDownTime >= 0);
timer2.text = Math.Round(countDownTime, 2).ToString();
}
}
我对其进行了更改,这是我的修复:
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && !hasStarted)
{
hasStarted = true;
StartGame();
}
DisplayTime();
}
void DisplayTime()
{
if (timerStart && countDownTime >= 0)
{
do
{
countDownTime -= Time.deltaTime;
} while (countDownTime >= 2);
timer2.text = Math.Round(countDownTime, 2).ToString();
}
}