I wonder if there is a simple way to use some kind om interval timing in XNA? For example call a method every 10 seconds?
I'm also wondering if there is a way to hide and unhide a sprite when it's on the screen?
Thanks!
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityTo expand on Jason's answer, I typically handle it by creating a couple variables:
float timer = 10; //Initialize a 10 second timer
const float TIMER = 10;
Then in the Update() method, you can then do something like this:
public void Update(GameTime gameTime)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
timer -= elapsed;
if (timer < 0)
{
//Timer expired, execute action
timer = TIMER; //Reset Timer
}
}
Create a variable:
bool drawSprite = true;
Set this variable in your code, depending on where you want to switch it on or off. You could set it inside of your timer action, so it would flip on/off every 10 seconds.
if (timer < 0)
{
drawSprite = !drawSprite;
timer = TIMER; //Reset Timer
}
Then in your Draw() function:
public void Draw()
{
if (drawSprite)
//Drawcode
}
You could have a timer variable, and add gameTime.ElapsedGameTime to it on every update().
As for your second question: in your draw() function, use an if statement for drawing sprites.