且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-17 16:18:10

要在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.