I have a simple game loop for a pacman clone, and want to add a timer.
After doing some research there are a few options. For example, I have read about a Win32 timer, a WIndows forms timer, and a system threading timer. With the latter the principle appears to be embedding the game loop inside timer events: http://csharptips.wordpress.com/2010/08/24/thread-timer/
This would imply that to count for a number of seconds I could record the number of game loop iterations.
But I wonder, what do people recommend? And what have people used? It's my first look into this subject, so any comments would be useful.
Cheers
Simple game timer - which method to use?
In the end I used the thread timer method. It's was very easy indeed.
secondsPassed = 0; // Reset the time
timer = new System.Threading.Timer(new System.Threading.TimerCallback(SecondHasPassed), null, 0, 1000);
private void SecondHasPassed(object obj)
{
secondsPassed++;
}
Problem solved
secondsPassed = 0; // Reset the time
timer = new System.Threading.Timer(new System.Threading.TimerCallback(SecondHasPassed), null, 0, 1000);
private void SecondHasPassed(object obj)
{
secondsPassed++;
}
Problem solved
In c#, int is not thread safe.
It is actually a Nullable<> struct consisting of an integer and a boolean.
http://stackoverflow...int-thread-safe
So if you do use an integer in this way, perhaps put a mutex lock around it. However, I probably recommend using the Winforms timer (System.Windows.Forms.Timer) or the timer provided with the c# language (System.Timers.Timer)
It is actually a Nullable<> struct consisting of an integer and a boolean.
http://stackoverflow...int-thread-safe
So if you do use an integer in this way, perhaps put a mutex lock around it. However, I probably recommend using the Winforms timer (System.Windows.Forms.Timer) or the timer provided with the c# language (System.Timers.Timer)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement