【发布时间】: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变量的值在哪里赋值? -
是的,游戏时间为空。