我是C#编程的新手,这是我第一次在XNA中使用它。我正在尝试与朋友一起制作游戏,但我们正在努力制作一个基本的计数器/时钟。我们需要的是一个定时器,从1开始,每2秒+1,最大容量为50.任何编码帮助都会很棒!感谢。
答案 0 :(得分:3)
要在XNA中创建计时器,您可以使用以下内容:
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的专家,所以我感谢任何提示/建议。
答案 1 :(得分:-1)
如果您不想使用XNA ElapsedTime,可以使用c#计时器。您可以在msdn reference for timer
找到相关的教程无论如何,这里有一些代码可以或多或少地做你想要的。
首先,您需要在课堂上声明类似的内容:
Timer lTimer = new Timer();
uint lTicks = 0;
static uint MAX_TICKS = 50;
然后你需要在你想要的时候启动计时器
private void InitTimer()
{
lTimer = new Timer();
lTimer.Interval = 2000;
lTimer.Tick += new EventHandler(Timer_Tick);
lTimer.Start();
}
然后在Tick事件处理程序中,你应该做你想做的每50个滴答。
void Timer_Tick(object sender, EventArgs e)
{
lTicks++;
if (lTicks <= MAX_TICKS)
{
//do whatever you want to do
}
}
希望,这有帮助。