[C++] Timer for shot delay

Started by
0 comments, last by blueapple 16 years, 10 months ago
I need a function to make a gun (in a game) shoot at determined intervals instead of continuously. I thought about setting a function ShootCheck() to be executed each game cycle, to verify if iTimer (an integer) has reached zero. If yes, the gun is allowed to shoot. If not: iTimer--; But I want to do it without any global variables. Is it possible? Please send me the code...
Advertisement
class Gun{public:    Gun()    {        lastFired = 0;    }    void shot()    {        if(lastFired - GetTime() >= 1000) // GetTime would return time in milliseconds        {            // BANG!            // Spawn a bullet and smoke, etc.            lastFired = GetTime();        }    }private:    int lastFired;}


Then just call the shot() function each update, it checks if it's has passed more than 1000 milliseconds (1 sec) since the last bullet was fired, if so, it shoots, else, it does nothing.
Check out my devlog.

This topic is closed to new replies.

Advertisement