Reset gameTime.TotalGameTime.Seconds to zero

Started by
1 comment, last by FantasyVII 11 years, 9 months ago
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);
}
}
}

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.

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. :)

This topic is closed to new replies.

Advertisement