當前的代碼是不工作的原因是調用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類。 –