Creating a steady rhythm for music-based game in XNA

Posted by A-Type on Game Development See other posts from Game Development or by A-Type
Published on 2012-06-04T01:51:15Z Indexed on 2012/06/04 4:49 UTC
Read the original article Hit count: 214

Filed under:

I'm looking to develop a game for Windows Phone to explore an idea I had which involves the user building notes into a sequencer while playing a puzzle game.

The issue I'm running into is that, while my implementation is very close to being on-beat, there is the occasional pause between beats which makes the whole thing sound sloppy. I'm just not sure how to get around this inside XNA's infrastructure.

Currently I'm running this code in the Update method of my GameBoard:

public void Update(GameTime gameTime)
{
    onBeat = IsOnBeat(gameTime);
    [...]
    if (onBeat)
        BeatUpdate();
}

private bool IsOnBeat(GameTime gameTime)
{
    beatTime += gameTime.ElapsedGameTime.TotalSeconds;
    if (Math.Abs(beatTime - beatLength) < 0.0166666)
    {
        beatTime -= beatLength;
        return true;
    }
    return false;
}

private void BeatUpdate()
{
    cursor.BeatUpdate();
    board.CursorPass((int)cursor.CursorPosition % Board.GRID_WIDTH);
}

Update checks to see if the time is on beat, and if it is, it calls the BeatUpdate method which moves the cursor over the board (sequencer). The cursor reports its X position to the board, which then plays any notes which are in that position on the sequencer. Notes are SoundEffectInstances, preloaded and ready to play. Oh, and TargetElapsedTime is set to 166666, or 60FPS target.

Obviously totaling up the time and then subtracting isn't the most accurate way to go but I can't figure out a way to work within XNA's system in order to overcome this issue. This current system is just horribly unstable. Beats lag and fire too early and it's obvious. I thought about perhaps some sort of threaded solution but I'm not familiar enough with multithreading to figure out how that would work. Any ideas?

© Game Development or respective owner

Related posts about XNA