Update(GameTime gameTime) - how to do task some specific times per second?
I'm using C# and XNA. And there's 开发者_运维百科this method in Game class
Update(GameTime gameTime)
I need to execute my function inside this method about 4 times per second. How can I acheive that?
So far I could only know when new second starts by doing
if (gameTime.TotalGameTime.Milliseconds == 0)
But I need a way to run my function some specific times per second. But I can't figure out how I can do it...
You need to keep a separate counter, and the interval at which to call your function. For 4 times a second, this is float interval = 1/4;
.
Every frame, update the counter by the number of milliseconds that have passed since the last frame.
Check if this counter is greater than interval; if so, at least interval
seconds have passed and the function needs to be called.
Another option may be using the Timer class from System.Timers
This class allows running recurring events in your application/game.
For more information, read here: http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
精彩评论