Jump to content
  • Advertisement
Sign in to follow this  
FantasyVII

Reset gameTime.TotalGameTime.Seconds to zero

This topic is 2257 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

hello guys,

so I have spent an hour searching google on how to reset gameTime.TotalGameTime.Seconds to zero. but from the looks of it you can't do that !!

why didn't microsoft just make a simple function ResetGameTime?!!!

SFML has it why XNA doesn't?

anyways here what i want to do. bascily I have a array of ten eneimes. and I want each enemy to spawn when time >=1. and then reset the time to 0;

so they don't spawn on top of each other.

here is my code:-


class MultipleEnemies
{
public Enemy[] enemy = new Enemy[10];

int SpawnNextEnemy;
SpriteFont Text;
double time;
public MultipleEnemies()
{
SpawnNextEnemy = 0;
}
public void Load(ContentManager Content)
{
for (int i = 0; i < 10 - 1; i++)
{
enemy = new Enemy();
enemy.LoadContent(Content);
Text = Content.Load<SpriteFont>("Fonts/Font");
}
}
public void Update(GameTime gameTime)
{
time = gameTime.TotalGameTime.Seconds;
for (int i = 0; i < SpawnNextEnemy; i++)
{
enemy.Update(gameTime);
}

if (time >= 1)
{
SpawnNextEnemy++;
time -= time;

if (SpawnNextEnemy >= 10)
SpawnNextEnemy = 9;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.DrawString(Text, "SpawnNextEnemy=" + SpawnNextEnemy + "\ntime" + time, new Vector2(250, 0), Color.White);

for (int i = 0; i < SpawnNextEnemy; i++)
{
enemy.Draw(spriteBatch);
}
}
}

Edited by FantasyVII

Share this post


Link to post
Share on other sites
Advertisement
What you could do is in MultipleEnemies(), set a member called lastTime=0. Then in Update() you would set time=gameTime.TotalGameTime.Seconds, then set a local variable deltaTime=time-lastTime. This would give the amount of time since the last time through the Update loop. If this value is greater than 1 second, increment SpawnNextEnemy. At the end of Update set lastTime=time. This way, you don't have to worry about setting your main game timer to 0. You are always keeping track of the last time Update was called, and using the elapsed time to determine whether to spawn an enemy.

Share this post


Link to post
Share on other sites

What you could do is in MultipleEnemies(), set a member called lastTime=0. Then in Update() you would set time=gameTime.TotalGameTime.Seconds, then set a local variable deltaTime=time-lastTime. This would give the amount of time since the last time through the Update loop. If this value is greater than 1 second, increment SpawnNextEnemy. At the end of Update set lastTime=time. This way, you don't have to worry about setting your main game timer to 0. You are always keeping track of the last time Update was called, and using the elapsed time to determine whether to spawn an enemy.


thank you very much. works perfectly. :)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!