简体   繁体   中英

XNA GameTime Windows Phone

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

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.

Figured it out, because this is specific to windows phone, you have to use the built in GameTimerEventArgs. Thanks for the help guys.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM