# How do I make a time lapse in my code so it updates slower?

I have made some collision if statements, but they didn't work.

            birdbox3.X += 5;
birdbox3.Y -= 5;

if (birdbox3.Intersects(Banner1)) {

birdbox3.Y += 10;

}
else if (birdbox3.Intersects(Banner2)) {

birdbox3.Y = -birdbox3.Y; }


So if we take the first statement, the box is initially on the left down corner. According to my code, it would ideally be going right+up in my game, once it hits the banner on the extreme top, it should go down+right, thus both the X and the Y positions will increase. But what happens is that is starts bouncing really fast, after debugging I realised it was forever stuck in the first if, it's almost as if it collides 2 times, reverting Y to its initial movement, colliding a 3rd time, and doing the process over and over.

Which brings us to the question, how can I make the Update code run a tad bit slower, or after half a second or such passes, so when it runs, it doesn't misinterpret collisions?

• A drawing of the situation would make things a lot clearer. – MichaelHouse Mar 2 '15 at 6:48

I have found the solution to this somewhere else. Adding a bigger if statement such as this :

private int elapsedTime = 0; //Declared at class level

public override void Update(GameTime gameTime)
{
elapsedTime += gameTime.ElapsedGameTime.TotalMilliseconds;

if (elapsedTime > 500)
{
elapsedTime = 0;
birdbox3.X += 5;
birdbox3.Y -= 5;

if (birdbox3.Intersects(Banner1))
{
birdbox3.Y += 10;
}
else if (birdbox3.Intersects(Banner2))
{
birdbox3.Y = -birdbox3.Y;
}
}
}


easily splits frames apart from each other, which helps with handling such issues, additionally this code sort of goes too far down the Y axis, so it goes further than the banner before it actually tests for it. I hope this helps anyone running into the same problem.

• I'm not sure artificially delaying your frame time is a good solution in general, but if there's no visible problems from doing this, it might be good enough in your case. – Christian Mar 2 '15 at 8:58
• a better way to do this is birdbox3.X += gameTime.ElapsedGameTime.TotalSeconds * 0.5f; instead. – dimitris93 Mar 5 '15 at 9:49