【问题标题】:GameTime is always 0 - MonoGame (XNA) - C#GameTime 始终为 0 - MonoGame (XNA) - C#
【发布时间】: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_ 属性(TotalSecondsTotalMilliseconds、...)。 “非总计”方法仅返回部分时间,而“总计”方法返回整个跨度,以您想要的时间单位表示。例如,如果经过 1.7 秒,Milliseconds 将返回 700,而 TotalMilliseconds 将返回 1700Seconds 将返回 1TotalSeconds 将返回 1.7。通常,只有在您想要进行特殊格式化或其他非标准格式时才使用“普通”属性。
  • 另请注意,您实际上不需要保留自己的累积计时器,GameTime 已经在 GameTime.TotalGameTime 属性中为您完成了这项工作。

标签: c# xna monogame xna-4.0


【解决方案1】:

您的_gameTime0,因为它刚刚被初始化,就是这样。

您的 Game 类(继承自 Game)有一个 Update 方法,参数类型为 GameTime。此参数是您当前的帧时间。所以你的UpdateTimer 也应该有一个GameTime 参数并且应该在你的Game.Update 中调用

public void UpdateTimer(GameTime gameTime)
{
    timer += gameTime.ElapsedGameTime;
    Debug.WriteLine("Timer: " + timer.TotalMilliseconds);
}

【讨论】:

  • 不要使用Milliseconds!使用TotalMillisecondsMilliseconds 每 1000 毫秒更新一次(如果 1500 毫秒在更新之间传递,它只会返回 500)。 TotalMilliseconds 将为您提供自上次更新以来的时间,以毫秒为单位。
  • 刚刚从他那里复制的,所以是的..我可以修复它。
【解决方案2】:

您需要将主游戏时间传递给实体。 ex-(Entity._gameTime = gametime) 然后开始更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2023-03-10
    • 2014-01-09
    相关资源
    最近更新 更多