Rhythm-based games and update consistency

Right now I’m starting to work on a simple rhythm platformer for Android and PC. The game world will update according to the beat of the music. At first I tried updating twice a second by having an int increment each update until it got to thirty, like this—

void Update()
	if (counter == 30)
		counter = 0;

—and for the most part, that works. However, I set it to play a tone each time counter reached thirty, and then listened to it for a while, and noticed little inconsistencies in the spacing between notes. There was other code involved in the update method, too, but nothing too drastic.

So I was wondering, what’s the best way to go about this? One way I thought of was to use some sort of Timer independent of the Update method, and then have the Update method spin until the timer interrupts. That would probably mean increasing how frequently Game1.Update() is called. How do I do that?

Also, if I were to use one of the .NET timers, would Stopwatch or Timer be more accurate?

The GameTime passed as a parameter to Game.Update is a more accurate way of accumulating elapsed time. Keep a class member of the type double and add gameTime.TotalSeconds to it each call to Update. When it reaches 30 (total time >= 30), play the sound then subtract 30 from totalTime.

Thanks, that’s good to know. I’ll try that out.

If your wanting half a second like I think you are you need to check if (totaltime > .5f)

Yeah, I figured it might be something like that. I have yet to try it out, but I’ll experiment with it.