Sign in to follow this  
asdqwe

[C++] Timer for shot delay

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this