0

I'am building a game in XNA, and I'am very new to XNA as well, but I got to start somewhere. Almost everything in my games works perfect. It is an topview racing game. The only problem is that I want to last the bonuses, that I pickup from the map last for a given time, like 3 seconds, I know that I need to use a timer, but I don't know how I should use it.

this is a part of code, I use when the car collides with a tile of my map:

 if (map[x][y] == 0)
        {
            if (speedBonus)
            {
                car.speed = 450;
            }
            else
            {
                car.speed = 200;
            }

I hope one of you guys can help me with this problem, because I have spent like 2 hours fixing it, but my code didn't help me out. Thanks in advance!

2 Answers 2

3

You'll want to record the time the bonus was picked up, which you can save into a DateTime object. Then, during each iteration of your main loop, check to see if the TimeSpan is greater than 3 seconds. If so, remove the bonus.

0

Thanks for the help, I fixed my code this way:

With 2 variables on top:

float timer = 3;
const float TIMER = 3;

And in the update:

if (map[x][y] == 0)
        {
            if (speedBonus)
            {
                float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
                timer -= elapsed;

                car.speed = 450;

                if (timer <= 0)
                {
                    speedBonus = false;
                    timer = TIMER;   //Reset Timer
                }
            }
            else
            {
                car.speed = 200;
            }

it now works like a charm, thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.