1
下面的snipet來自我的Form1代碼。我一直因爲我的MessageBox.Show(...)而得到格式異常;聲明在我的Stop()方法中。爲什麼?我究竟做錯了什麼? ...格式異常
private TimeSpan iterationDuration = TimeSpan.Zero;
...
public void Stop()
{
IsGameOver = true;
MessageBox.Show(String.Format("Game Over\n\nScore = {0}\n\n Time Duration = {l}", score, iterationDuration));
Application.Exit();
}
public void Start()
{
score = 0;
IsGameOver = false;
currentRedLightX = 0;
currentRedLightY = 0;
currentGreenLightX = width/2;
currentGreenLightY = height/2;
double minIterationDuration = SPEED; // 50 frames/sec
//game loop
while (!IsGameOver)
{
if (IsCollision())
{
score += 10;
}
DateTime startIterationTime = System.DateTime.Now;
UpdateGameState();
Render();
DateTime endIterationTime = System.DateTime.Now;
TimeSpan iterationDuration = endIterationTime - startIterationTime;
if (iterationDuration.TotalMilliseconds < minIterationDuration)
Thread.Sleep(Convert.ToInt32(minIterationDuration - iterationDuration.TotalMilliseconds));
Application.DoEvents();
}
}
你的意思是在你的遊戲循環聲明不同的`iterationDuration`?因爲你只是在間隔後面,或者最好還是`System.Diagnostics.Stopwatch`,所以你最好使用'DateTime.UtcNow`而不是`.Now`(避免時區和DST計算)。 – Rup 2010-11-25 17:52:38
少一點的代碼和一點點的錯誤信息會有所幫助。 – 2010-11-25 17:55:49