I want that my ship shoots beam after every 2 seconds. How do I make that happen? and also I want that after 50 shots I want to give 10 seconds to reload the gun. I know both of my problems can be solved by gameTime.ElapsedGameTime but how should I use it?

  • 1
    \$\begingroup\$ I like using timers with callbacks personally. xnawiki.com/index.php/Event_Driven_Timer \$\endgroup\$ – ClassicThunder Aug 26 '13 at 15:21
  • \$\begingroup\$ I would avoid using the elapsed time like this. You should do physics at a fixed frequency to make things deterministic. For instance, if you know the ship is in a state where it is shooting lasers and 15 seconds have elapsed since your last update (ouch) you would know to shoot 7 lasers and you would even know that the first one should have had 15*Hz-many position updates since the last timed update. But if you simply use the condition: shoot 1 laser if at least N-many seconds has elapsed, things get dicy. Think of shooting lasers (very nearly) every 2 seconds and not after every 2 seconds. \$\endgroup\$ – Andon M. Coleman Aug 26 '13 at 23:26

You can use gameTime in the following manner along with 2 accumulator variables to store the time since the last shot and the number of shots taken since reload.

gameTime.ElapsedGameTime.TotalSeconds represents the elapsed time since the last time the Update method was called (the time since last frame). If you accumulate each of these times (add them up each frame), they will eventually add up to the 2 second interval that you want to wait until your next shot. Once the shot has been fired, reset that accumulator back to zero and wait for it to add up to 2 again.

Additionally, you can keep track of the number of shots each time a shot was fired and when that number gets to 50, reset that timeSinceLastShot accumulator to -8. That way, it will take 10 seconds before the accumulator reaches the 2 second mark to initiate a new shot.

//class scope field
int numShotsFromCurrentMagazine = 0;
float timeSinceLastShot = 0f;

//in the update method
timeSinceLastShot += (float)gameTime.ElapsedGameTime.TotalSeconds;

if(timeSinceLastShot > 2f)//if it's been 2 seconds since this was reset to zero, initiate another shot

if(numShotsFromCurrentMagazine == 50)
  timeSinceLastShot = -8f;//since magazine is empty, set accumulator to -8 so it will be 10 seconds before it gets to the required 2 and initiate the next shot
  numShotsFromCurrentMagazine = 0;//reset the magazine.

//stand alone method
void Shoot()
  //code that shoots beam here
  numShotsFromCurrentMagazine += 1;
  timeSinceLastShot = 0f;
|improve this answer|||||
  • 5
    \$\begingroup\$ Since this is a very beginner level question, including text that explains the rationale of the code should be included. Code only answers are not very good answers. \$\endgroup\$ – MichaelHouse Aug 26 '13 at 15:32
  • \$\begingroup\$ Thanks Byte, you are right of course, comment appreciated. Answer edited as suggested. \$\endgroup\$ – Steve H Aug 26 '13 at 23:54
  • \$\begingroup\$ You might want to make "timeSincelastShot" function as a cool down timer rather than a counter to avoid some weird math issues. Also you should make some of these hardcoded variables just to simplify making changes (variables like numShootsFromCurrentMagazine, the 2.0f delay, and the 10.0f reload time) \$\endgroup\$ – Benjamin Danger Johnson Aug 27 '13 at 0:12
  • \$\begingroup\$ Thanks this help a lot, I was really confused in using gametime class, now it has become clear. Thanks alot. \$\endgroup\$ – Hussain Murtaza Aug 28 '13 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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