【发布时间】:2017-10-12 11:24:35
【问题描述】:
目前,我的精灵循环速度与 MonoGame 循环其代码的速度相同。我想通过使用 GameTime 创建延迟来减慢这个过程。然而,它从来没有奏效,所以我决定使用 debug.WriteLine() 来检查 GameTime 是否更新。从来没有。
abstract class Entity
{
protected GameTime _gameTime;
public TimeSpan timer { get; set; }
public Entity()
{
_gameTime = new GameTime();
timer = _gameTime.TotalGameTime;
}
public void UpdateTimer()
{
timer += _gameTime.TotalGameTime;
Debug.WriteLine("Timer: " + timer.Milliseconds);
}
}
UpdateTimer() 在游戏循环中连续运行,但始终在调试输出中写入“0”。
我做错了什么?或者有更好的方法吗?
【问题讨论】:
-
一般注意事项:在处理
GameTime时,您应该(几乎)始终使用Total_属性(TotalSeconds、TotalMilliseconds、...)。 “非总计”方法仅返回部分时间,而“总计”方法返回整个跨度,以您想要的时间单位表示。例如,如果经过 1.7 秒,Milliseconds将返回700,而TotalMilliseconds将返回1700。Seconds将返回1,TotalSeconds将返回1.7。通常,只有在您想要进行特殊格式化或其他非标准格式时才使用“普通”属性。 -
另请注意,您实际上不需要保留自己的累积计时器,
GameTime已经在GameTime.TotalGameTime属性中为您完成了这项工作。