gavin beard

hi all,

in my main draw function of my game i call a function splash.Draw(gameTime);

in the splash.Draw() function i have a line of code

Console.WriteLine((float)gt.ElapsedRealTime.Seconds);

this along with every other member of gt just returns zero, i cant figure n e reason this should happen, if i call the same function in the main Update or draw functions of the game it still returns zero, is there a reason it should do this

many thanks



Re: XNA Game Studio Express game time

Codata

Hi,

The way i do this is as follows:

public class Game1 : Microsoft.Xna.Framework.Game
{
float elapsed = 0;
}

protected override void Update(GameTime gameTime)
{
elapsed += (float)gameTime.ElapsedRealTime.TotalSeconds;
Console.WriteLine(elapsed);
}


hope this helps
Codata





Re: XNA Game Studio Express game time

B1narySh0t

You have to remember that gameTime.ElapsedRealTime.Seconds will return a WHOLE number representing the number of seconds that have passed since the last call to the Draw function.

It will always return zero (unless you are doing something that causes draw to take a long time in between calls).

A more accurate timer could be to retrieve the milliseconds that have passed.

float elapsed = 0;

elapsed += (float)gameTime.ElapsedRealTime.Milliseconds / 1000.0f;

You have to divide the Milliseconds by 1000.0f because Milliseconds returns an integer representing how many milliseconds have passed since the last call to the Update or Draw function (wherever you place this code).

If you wanted to wait 2 second before proceeding, then simply check:

if (elapsed > 2.0f)
//proceed with moving on to the next game state
else
elapsed += (float)gameTime.ElapsedRealTime.Milliseconds / 1000.0f;





Re: XNA Game Studio Express game time

gavin beard

Codata wrote:

Hi,

The way i do this is as follows:

public class Game1 : Microsoft.Xna.Framework.Game
{
float elapsed = 0;
}

protected override void Update(GameTime gameTime)
{
elapsed += (float)gameTime.ElapsedRealTime.TotalSeconds;
Console.WriteLine(elapsed);
}


hope this helps
Codata

many thanks, but it still just prints 0 everytime the function is called





Re: XNA Game Studio Express game time

Codata

Hi,
Do you have the
Float elapsed =0;
in the public

What this is doing is creating a flot for the seconds total to be stored.

When you are reporting you need do have a global variable to increment wverytime it goes into the update as it will only report the last time it was in the update.

If you can send us your code i will take a look, Are you looking for total game time or as some1 posted just do something when it gets to a certain seconds value

Thanks
Gareth





Re: XNA Game Studio Express game time

Peter D.

Try this

// place me at the top of the class along with the other private members
private float elapsed;

public class Game1 : Microsoft.Xna.Framework.Game
{
    elapsed = 0;
}

protected override void Update(GameTime gameTime)
{
elapsed += (float)gameTime.ElapsedGameTime.Milliseconds;

Console.WriteLine(elapsed / 1000);
}







Re: XNA Game Studio Express game time

gavin beard

Peter D. wrote:
Try this

// place me at the top of the class along with the other private members
private float elapsed;

public class Game1 : Microsoft.Xna.Framework.Game
{
elapsed = 0;
}

protected override void Update(GameTime gameTime)
{
elapsed += (float)gameTime.ElapsedGameTime.Milliseconds;

Console.WriteLine(elapsed / 1000);
}


strange, not sure what it was but now works fine without n e code changes, thanks alot to all