如何在C#XNA中创建计时器/计数器

问题描述:

我对C#编程还很陌生,这是我第一次在XNA中使用它.我正在尝试与朋友一起制作游戏,但我们一直在努力制作基本的计数器/时钟.我们需要的是一个从1开始的计时器,每2秒+1,最大容量为50.编码方面的任何帮助都是非常棒的!谢谢.

I'm fairly new to C# programming, and this is my first time using it in XNA. I'm trying to create a game with a friend, but we're struggling on making a basic counter/clock. What we require is a timer that starts at 1, and every 2 seconds, +1, with a maximum capacity of 50. Any help with the coding would be great! Thanks.

要在XNA中创建计时器,您可以使用以下方法:

To create a timer in XNA you could use something like this:

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

currentTime += (float)gameTime.ElapsedGameTime.TotalSeconds; //Time passed since last Update() 

if (currentTime >= countDuration)
{
    counter++;
    currentTime -= countDuration; // "use up" the time
    //any actions to perform
}
if (counter >= limit)
{
    counter = 0;//Reset the counter;
    //any actions to perform
}

我绝不是C#或XNA方面的专家,因此,我感谢任何提示/建议.

I am by no means an expert on C# or XNA as well, so I appreciate any hints/suggestions.