【问题标题】:Creating a timer in Monogame. Creating an event after 5 seconds在 Monogame 中创建计时器。 5 秒后创建事件
【发布时间】:2019-12-09 11:26:19
【问题描述】:

我在为我的 c# monogame 创建计时器时遇到了一些麻烦。我已经在堆栈溢出时将其他计时器问题变红了,但我没有得到它。 我想在我的矿井里收集黄金。但是金币是有限的,每5秒应该有新的金币。 我试着像How to create a timer/counter in C# XNA那样做 但它不起作用。我得到一个 System.NullReferenceException。有没有更好的方法来做计时器?或者我该如何修复异常?

这是我的代码中最重要的部分:

private GameTime gameTime;
int counter = 1;
int limit = 50;
float countDuration = 10f; //every  2s.
float currentTime = 0f;

private void CollectGold(ObjectFactory.ObjectType type)
{ 
    currentTime += (float)gameTime.ElapsedGameTime.TotalSeconds; //Time passed since last Update()
    if (currentTime >= countDuration)
    {
        counter++;
        currentTime -= countDuration;
        // maxGold limits the Mine 
        if (mMaxGold > 0)
        {
            mGold += 5;
            mMaxGold -= 10;
         }
    }
}

【问题讨论】:

  • 究竟是什么不起作用..我不熟悉monogame,XNA和unity..究竟什么是空的? gameTime 可能是我的猜测......
  • gameTime 变量的值在哪里赋值?
  • 是的,游戏时间为空。

标签: c# time counter monogame


【解决方案1】:

谢谢。我做到了。只是将 GameTime 的值赋值错了。不知道它必须在更新功能中。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-09
    • 2011-06-18
    • 1970-01-01
    • 2013-05-24
    • 2020-10-27
    • 2019-10-01
    • 2016-07-11
    • 1970-01-01
    相关资源
    最近更新 更多