I'm making a little 2D tank game that fires bullets depending on where you click (This has been done and works a treat).

I'm currently working on a timer class with a simple function that displays a little countdown depending on the inputted time. (This will be used for bullet fire delays).

bool timer makeTimer(int waitTime)

This is the start of the timer, it is a simple boolean function which returns "true" when the timer has reached 0.

I can make it decrement down until it reaches 0 easily, but the part I'm having issues with is making it countdown based on an actual time event, so once per second. I've tried lots of different things but I just can't get this darn part to work.

The variables I have to work with are;

waitTime // inputted timeDelay
sf::Clock // To show the elapsed time per frame
sf::seconds(1) // Not too sure if I need this

So, to wrap up. - I'm trying to get it to countdown from the inputted time once per second.

  • \$\begingroup\$ Please don't provide answers in your question. If you want to provide an answer, post it below with the other answers. \$\endgroup\$
    – House
    Jul 18, 2014 at 12:55

2 Answers 2


SFML2 allows arithemic operations on sf::Time, like substracting or comparing timestamps. It will be more reliable and consistent to compare elapsed time with desired delay, rether than counting down, especially integer of milliseconds(?).

Things like repeated shots are easier (imo) to implement, if you treat fire delays as locks, i.e. next bullet will not fire until enough time passed since previous shot.

sf::Clock game_clock;
sf::Time shot_delay = sf::seconds(0.25);
sf::Time last_fire_timestamp;

sf::Time now = game_clock.getElapsedTime();
    if((now - last_fire_timestamp) >= shot_delay)
        last_fire_timestamp = now;
  • \$\begingroup\$ That is what I'm actually doing. I made my function a boolean so it is really easy to use in situations like this. I can basically do if(timer==true){ bullet.fire(); } Though my original function name isn't the best, it should be called coolDown not makeTimer I do like this version of doing things though. Will do some tests on it. Marked as accepted. \$\endgroup\$ Jul 18, 2014 at 11:03

Subtract the elapsed time for the current frame from the timer's current value. When the timer value goes zero or negative, you know time's up.

Assuming SFML 2.0, you can do:

sf::Time elapsed = clock.restart();
timer.value -= elapsed.asMilliseconds();

...or elapsed.asSeconds(), or .asMicroseconds(), as you like; whatever units your timer uses.

  • \$\begingroup\$ Managed to get what I wanted, will update my original post with the answer \$\endgroup\$ Jul 18, 2014 at 10:37
  • \$\begingroup\$ @Johnathan Happy if I helped. I think you should post the final solution as an answer (instead of a question edit), just to keep things neatly separated and clear for future visitors. \$\endgroup\$
    – Anko
    Jul 18, 2014 at 11:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .