英文:
How do I stop seconds displaying 60 before going back to 0
问题
我试图在玩家的用户界面中显示时间,但似乎无法阻止秒数在返回0之前显示为60。我以为它会在显示任何内容之前首先执行if语句中的所有操作,但似乎不是这样。以下是我的代码:
private void Update() {
seconds += timeMultiplier * Time.deltaTime;
if (seconds >= 60) {
seconds -= 60;
minutes += 1;
} else if (minutes >= 60) {
minutes -= 60;
hours += 1;
} else if (hours >= 24) {
hours -= 24;
days += 1;
} else if (days >= 7) {
days -= 7;
weeks += 1;
} else if (weeks >= 52.17857f) {
weeks -= 52.17857f;
years += 1;
}
}
public string DisplayTimeInformation() {
StringBuilder builder = new StringBuilder();
builder.Append(hours.ToString("#00")).Append(":").Append(minutes.ToString("#00"));
if (displaySeconds) {
builder.Append(":").Append(seconds.ToString("#00")).Append(" ");
} else {
builder.Append(" ");
}
return builder.ToString();
}
我尝试使用一个名为displayedSeconds
的第二个变量,它与正常的seconds
变量相同,并将其限制在59,尽管它在技术上仍然是准确的,但它会在59停留一会,然后从00加速到01,然后再返回正常,这看起来不太好看。
英文:
I'm trying to display the time in the player's UI but I can't seem to stop the seconds displaying 60 before it goes back to 0. I thought it would do everything in the if statement first before displaying anything, but that doesn't seem to be the case. Here is my code:
private void Update() {
seconds += timeMultiplier * Time.deltaTime;
if (seconds >= 60) {
seconds -= 60;
minutes += 1;
} else if (minutes >= 60) {
minutes -= 60;
hours += 1;
} else if (hours >= 24) {
hours -= 24;
days += 1;
} else if (days >= 7) {
days -= 7;
weeks += 1;
} else if (weeks >= 52.17857f) {
weeks -= 52.17857f;
years += 1;
}
}
public string DisplayTimeInformation() {
StringBuilder builder = new StringBuilder();
builder.Append(hours.ToString("#00")).Append(":").Append(minutes.ToString("#00"));
if (displaySeconds) {
builder.Append(":").Append(seconds.ToString("#00")).Append(" ");
} else {
builder.Append(" ");
}
return builder.ToString();
}
I tried using a second displayedSeconds
that is the same as the normal seconds
variable and clamping it to 59, and even though it was technically still accurate, it would linger on 59 for a bit and then speed up from 00 to 01 before returning to normal which just doesn't look nice.
答案1
得分: 5
不需要计算所有这些时间属性。记录某事开始的时间会更容易:
var startedOn = DateTime.Now;
然后每次显示持续时间时执行:
var difference = DateTime.Now - startedOn;
这将为您提供一个方便显示的TimeSpan
对象:
var timeInString = difference.ToString(@"hh\:mm\:ss");
请参阅这里了解如何指定您自己的格式字符串。
英文:
There is no need to calculate all of those time properties. It's much easier to record the time something started:
var startedOn = DateTime.Now;
and then every time you display the duration do:
var difference = DateTime.Now - startedOn;
Which gives you a nice TimeSpan
object that you can display easily:
var timeInString = difference.ToString(@"hh\:mm\:ss");
See here for how you can specify your own format string.
答案2
得分: 2
更容易使用TimeSpan进行计算。您可以使用这个类来根据deltaTime和timeMultiplier获取小时、分钟和秒:
public class Timer
{
public int Seconds => timeSpan.Seconds;
public int Minutes => timeSpan.Minutes;
public int Hours => timeSpan.Hours;
private TimeSpan timeSpan;
public void Update(double timeMultiplier, double deltaTime)
{
timeSpan = timeSpan.Add(TimeSpan.FromSeconds(timeMultiplier * deltaTime));
Console.WriteLine(timeSpan.ToString(@"hh\:mm\:ss"));
}
}
英文:
It is more easy to use TimeSpan for calculations. You can use this class for obtaining hours, minutes and seconds depens on deltaTime and timeMultiplier:
public class Timer
{
public int Seconds => timeSpan.Seconds;
public int Minutes => timeSpan.Minutes;
public int Hours => timeSpan.Hours;
private TimeSpan timeSpan;
public void Update(double timeMultiplier, double deltaTime)
{
timeSpan = timeSpan.Add(TimeSpan.FromSeconds(timeMultiplier * deltaTime));
Console.WriteLine(timeSpan.ToString(@"hh\:mm\:ss"));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论