Reputation: 2445
I have been following the tutorial on Microsofts website and they use GameTime to keep track of a few things. However when I create a new instance, it does not start counting and there is no function like .start() to start the timer. When I run the game the created object gameTime from GameTime remains at 0 for every element inside. Does anyone know how to use GameTime properly because obviously, I am doing it wrong ;)
-Thanks
Upvotes: 1
Views: 616
Reputation: 11
I use this code for XNA wp game to countdown. I think this can also use in normal XNA GameTimer for countdown and time count.
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.
Upvotes: 1
Reputation: 2445
Figured it out, because this is specific to windows phone, you have to use the built in GameTimerEventArgs. Thanks for the help guys.
Upvotes: 1
Reputation: 7961
You should not be creating an instance of the GameTime
class and instead be using the gameTime
parameter provided to the Draw()
and Update()
methods of the Game
class:
TimeSpan totalGameTime = gameTime.TotalGameTime;
A good reference for Windows Phone 7 is Programming Windows Phone 7, a free e-book by Charles Petzold.
Upvotes: 4