Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Reset gameTime.TotalGameTime.Seconds to zero


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 FantasyVII   Members   -  Reputation: 268

Like
0Likes
Like

Posted 15 July 2012 - 07:57 AM

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[i] = new Enemy();
				enemy[i].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[i].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[i].Draw(spriteBatch);
			}
		}
	}


Edited by FantasyVII, 15 July 2012 - 08:10 AM.


Sponsor:

#2 JTippetts   Moderators   -  Reputation: 5059

Like
1Likes
Like

Posted 15 July 2012 - 08:43 AM

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.

#3 FantasyVII   Members   -  Reputation: 268

Like
0Likes
Like

Posted 15 July 2012 - 09:47 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS