当前的代码是不工作的原因是调用Thread.Sleep()
停止当前线程上的任何执行,直到给出的时间已过。所以,如果你Countdown.Start()
在游戏主线程(我猜你正在做的),你游戏将冻结,直到Sleep()
调用完成。
相反,你需要使用System.Timers.Timer
看一看的MSDN documentation。
UPDATE:现在希望更符合您的方案
public class Timer1
{
private int timeRemaining;
public static void Main()
{
timeRemaining = 120; // Give the player 120 seconds
System.Timers.Timer aTimer = new System.Timers.Timer();
// Method which will be called once the timer has elapsed
aTimer.Elapsed + =new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 3 seconds.
aTimer.Interval = 3000;
// Tell the timer to auto-repeat each 3 seconds
aTimer.AutoReset = true;
// Start the timer counting down
aTimer.Enabled = true;
// This will get called immediately (before the timer has counted down)
Game.StartPlaying();
}
// Specify what you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
// Timer has finished!
timeRemaining -= 3; // Take 3 seconds off the time remaining
// Tell the player how much time they've got left
UpdateGameWithTimeLeft(timeRemaining);
}
}
你能告诉我们更多的代码,例如你如何设置更新和绘制循环? – TJHeuvel
是滴答定时器,用于控制玩家通过迷宫玩了多少时间? – nbz
@Reinan - 您所有的代码都会将调用线程(您的应用程序)置于睡眠状态3秒钟。你需要一个独立的线程,让它进入睡眠状态,等待它醒来,然后再做任何事情。只需使用Timer类。 –