I was trying three different ways of using ElapsedGameTime and TotalGameTime, because I want an exact match, so everything is updated/drawn accordingly.
As I was experimenting, I learned that the very first update, ElapsedGameTime and TotalGameTime are both 0.
Second update, ElapsedGameTime is 0.0166667, which is correct (60 updates per second). But TotalGameTime is 0, I don't understand why.
So, if I start to add from the third update (time += gameTime.ElapsedTime), ElapsedGameTime is equal to TotalGameTime, otherwise there will be always a 0.0166667 difference.
Can someone explain that to me?
UPDATED: code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;
namespace TestTime
{
class TimeTest2
{
TimeSpan totalTimeElapsed;
TimeSpan frequency = TimeSpan.FromSeconds(5.5f);
int times = 0;
int timesCheckpoint = 1;
public void load()
{
}
public void initialize()
{
totalTimeElapsed = TimeSpan.Zero;
}
public void update(GameTime gameTime)
{
times++;
String debug = "";
TimeSpan zero = TimeSpan.Zero;
if( times > 2 )
{
totalTimeElapsed += gameTime.ElapsedGameTime;
}
if( totalTimeElapsed != gameTime.TotalGameTime )
{
debug += " Diff time:"+times+" ["+totalTimeElapsed.ToString() + " != " + gameTime.TotalGameTime.ToString() + "]";
}
TimeSpan checkpoint = TimeSpan.FromSeconds(5.5f*timesCheckpoint);
if( gameTime.TotalGameTime >= checkpoint )
{
debug += "5.5f MARK ";
timesCheckpoint++;
}
if( !debug.Equals("") )
{
addDebug(debug + " -" + gameTime.TotalGameTime.ToString());
addDebug("");
}
}
public void draw()
{
}
public void addDebug(string str)
{
Debug.WriteLine(str);
}
}
}
GameTime.TotalGameTime.Milliseconds, orGameTime.TotalGameTime.TotalMilliseconds? – Joe Apr 19 '12 at 20:18