• Advertisement
Sign in to follow this  

TimeSpan and GameTime

This topic is 2737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to have something happen every Xms in game time. To do this I created a TimeSpan (ts) object initialized to 0. In my update method, I add gameTime.ElapsedGameTime onto ts. Then I compare against ts.Milliseconds.

When I pass the desired amount on ms, I need to reset ts to 0. But Milliseconds is read only. I don't see a way of zeroing out a TimeSpan object without creating a new one.

Is there a different object I should be using to track passed time?

Share this post


Link to post
Share on other sites
Advertisement
In the simplest case, you could use a float or double to just track elapsed milliseconds. When your Update function is called, add ElapsedGameTime.TotalMilliseconds to your float/double variable.

In a more complex case, you could keep using the TimeSpan for clarity and do something like this:

// Declaration (class member variable)
TimeSpan nextEventTime; // measured in TotalGameTime.

// Reset the event (wherever you want to reset it)
nextEventTime = gameTime.TotalGameTime + new TimeSpan(0,0,0,0, milliseconds);

// Test the event (in Update method)
if (gameTime.TotalGameTime >= nextEventTime)
{
// do whatever.
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement