[英]Timer doesn't stop after changing bool to false in IF statement
I have this strange problem.我有这个奇怪的问题。 When I call stopTimer(), it changes the bool, but UpdateTime is still working and timer is going.
当我调用 stopTimer() 时,它会更改 bool,但 UpdateTime 仍在工作并且计时器正在运行。 Everything works, but the timer doesnt stop.
一切正常,但计时器不会停止。
public float elapsedTime;
public float startTime;
public bool stopTimer = false;
private bool keepTiming = true;
void Start()
{
startTime = Time.time;
}
void Update()
{
if(keepTiming)
{
UpdateTime();
}else{Debug.Log("Timer has stopped");}
if(stopTimer)
Debug.Log(StopTimer());
}
public void UpdateTime()
{
elapsedTime = Time.time - startTime;
float t = Time.time - startTime;
string minutes = ((int) t/ 60).ToString();
string seconds = (t % 60).ToString("f2");
timeText.text = minutes + ":" + seconds;
}
float StopTimer()
{
keepTiming = false;
return elapsedTime;
}
}
It worked by adding static to keepTiming bool.它通过向 keepTiming bool 添加静态来工作。 I don't really understand why, but it worked.
我真的不明白为什么,但它奏效了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.