如何在C#XNA中创建计时器/计数器 [英] How to create a timer/counter in C# XNA

查看:88
本文介绍了如何在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.

这篇关于如何在C#XNA中创建计时器/计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆