1 Replies - 995 Views - Last Post: 11 March 2016 - 11:09 AM

#1 Mr n1nja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-March 16

XNA timer

Posted 10 March 2016 - 06:46 AM

If i wanted to execute some code for a certain amount of time, say 5 seconds, then i want that code to stop, how would i do this?
I've tried this already.
timer = new TimeSpan(0, 0, 5);
                    //for total of 5 seconds game time do...

                    if (timer > TimeSpan.Zero)
                    {
                        timer -= gameTime.ElapsedGameTime;
                        //speed  up by 5 times for 10 seconds
                        speed = speed * 5;
                        speed = speed * 5;
                     }



                        if (timer <= TimeSpan.Zero)
                        {
                            speed = speed / 5;
                            speed = speed / 5;
}


Now the speed does increase here by 10 seconds but it doesn't only run for 10 seconds

Is This A Good Question/Topic? 0
  • +

Replies To: XNA timer

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: XNA timer

Posted 11 March 2016 - 11:09 AM

In XNA, you probably do not want to use Timers. This kind of goes against the way XNA is designed to work.

You probably know that the Game1.cs file has several standard methods and that there is a frame loop. You probably know that every frame it calls the Update and Draw methods over and over again until you stop the program. And you probably know that the Draw method is supposed to be the code to draw the screen every frame and the Update method is supposed to be where all your other code that runs while the game is running that is not Draw code is put.

The "Update(GameTime gameTime)" method has GameTime for a parameter. That is your timer. It gives you the amount of time that has passed since the last frame occurred. XNA has a parameter that forces 60 frames every second if the computer is capable of running faster. If you turn this off or if it runs slower, you can get different values.

But using this value you can implement timers. If you want something to occur after 5 seconds, you could have a variable keep track of the time that has passed. It starts at zero and you add the elapsed game time each frame. You check it to see if it exceeds five seconds and trigger your event when it does while resetting the value back to zero. Then it will fire every 5 seconds. You could have a flag that turns it on and off so that it only adds to the timer value if the flag is set to true. And you could decide whether to reset it to zero when the flag is changed or not.

Sorry, looks like that's largely what you're doing; I'm real bad about not looking at the code until after I've "answered" the question.

Anyway, I probably would not change the speed there. I would probably use a "SpeedUp" boolean flag. And I would take the spot in the code that consumes the speed and do an if-then to do the calculation one way if SpeedUp is true and another way if it is false. Then the flag is the determining factor and it's just a matter of switching the flag on and off. And if there's a problem with the speed calculation, you would then know where it's at.

I suppose it's up to you whether you count up or down, but either way, don't forget to reset your timer. Otherwise, you could be in an infinite loop of sorts or at least have a permanent condition.

This post has been edited by BBeck: 11 March 2016 - 11:15 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1