我有一个迷宫游戏。按Enter键后,您可以输入作弊码,同时,计时器将暂停。但是在输入代码后,我的计时器恢复,但它每秒递减3倍。下面是按条件输入:问题在暂停/恢复定时器
// gt.setTimer() is called at the moment the maze started
// I'm using getch to trap inputs
else if (move == 13) //Reads if Enter is pressed
{
pause = 1; //A Flag saying that the timer must be paused
gt.setTimer(pause); //Calls my setTimer() method
Console.Write("Enter Cheat: ");
cheat = Console.ReadLine();
pause = 0; //A Flag saying that the timer should resume
gt.setTimer(lives, pause); //Calls again the Timer
}
这里是我的SetTimer()代码:
static System.Timers.Timer t = new System.Timers.Timer();
static int gTime = 300;
public void setTimer(int pause)
{
t.Interval = 1000; // Writes the time after every 1 sec
if (pause == 1)
t.Stop(); // Stop the timer if you press Enter
else
t.Start(); // Starts the timer if not
t.Elapsed += new ElapsedEventHandler(showTimer);
}
public static void showTimer(object source, ElapsedEventArgs e)
{
Console.Write("Time " + gTime); //Writes time
gTime--; //Decrements the time
}
有什么错吗?我错过了什么吗?
为什么暂停一个int,而不是一个布尔值? – CodesInChaos
你的同步代码在哪里?您正在使用没有同步的多线程。海事组织你不应该在这里使用多线程。 – CodesInChaos