0
\$\begingroup\$

I am creating a game in allegro5 and I have come across a problem. I have a character that fires a shotgun and there needs to be a delay before he can shoot again. The code for shooting his gun is in a function called

fire_shotgun();

and I cant find a way to create a pause before he shoots again without stopping the whole game by using

al_rest();

I am wondering if there is any way to do this.

P.S. my game is running on a 60FPS timer if that is of any use.

\$\endgroup\$
1
  • \$\begingroup\$ The answer below is the correct answer, al_rest() just pauses the entire thread it currently runs till the time inside it runs out. It usually should not be used for gameplay but to pause threads for whatever reason you would need to do that (sync/delay action/initialize something/etc) \$\endgroup\$ Commented Feb 16, 2016 at 12:40

1 Answer 1

5
\$\begingroup\$

There are a couple of ways to approach this. One is to simply count down from the last time the gun was fired. If you have an update function that gets called every frame, you can do something like:

void update(float timeElapsed) {
    ...
    cooldown -= timeElapsed;
    ...
}

...

void fireShotgun() {
    if (cooldown <= 0) {
        createBullets();
        playSound();
        // etc...
    }
}

Another approach is to create a new ALLEGRO_TIMER that will notify you when the player is ready to fire again. This will likely be more complicated than the countdown approach.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.