2012-04-11 95 views
1

我一直在關注Microsofts網站上的教程,他們使用GameTime來跟蹤一些事情。但是,當我創建一個新實例時,它不會開始計數,並且沒有像.start()這樣的函數來啓動計時器。當我運行遊戲時,GameTime中創建的對象gameTime對於每個元素都保持爲0。有誰知道如何使用正確GameTime因爲很明顯,我做錯了;)XNA GameTime Windows Phone

-Thanks

+0

我個人總是使用異步/多線程繪圖操作,並且從未見過需要GameTime類。 – IDWMaster 2012-04-11 01:47:06

回答

4

你不應該創建GameTime類的實例,而是使用提供給Draw()gameTime參數和Game類的Update()方法:

TimeSpan totalGameTime = gameTime.TotalGameTime; 

Windows Phone的一個很好的參考7 Programming Windows Phone 7,一個免費的電子圖書由Charles Petzold的。

+0

即使我把TimeSpan totalGameTime = gameTime.TotalGameTime;在我的OnUpdate函數中,totalGameTime對於固定時間幀仍然讀取00:00 – 2012-04-11 03:55:11

+1

@that_guy:第1幀應該讀取0.第2幀可能讀取第0幀。第3幀讀取〜0.016。框架4它讀取〜0.032 ...等.. – 2012-04-11 12:43:08

1

想通了,因爲這是特定於Windows手機,你必須使用內置GameTimerEventArgs。謝謝你們的幫助。

+1

這不符合你原來的問題。如果您在某個特定事件中遇到問題,您應該在您的問題中提及此問題。 – Bernard 2012-04-12 00:25:34

1

我使用此代碼進行XNA wp遊戲倒計時。我認爲這也可以在正常的XNA GameTimer中用於倒計時和計時。

GameTimer gameTime = new GameTimer(); 
int _timer = 5; 
// initiate 
gameTime.UpdateInterval = TimeSpan.FromSeconds(1); // game time count every second 
gameTime.Update +=new EventHandler<GameTimerEventArgs>(gameTime_Update); // each time count, will exeute gameTime_Update method. 
// each time execute, will decrease _timer by 1 
void gameTime_Update(object sender, GameTimerEventArgs e) 
{ 
      _timer-= 1; 
} 
// on function OnNavigatedTo, after 
timer.Start(); // this is default 
gameTime.Start(); // this what I add.