Sign in to follow this  
jagguy2

single fire and repeated fire

Recommended Posts

How do i code for the situation I want an instant fire from a key and also a repeated fire? I dont want the missiles to be bunched up and look like 2 at once. I cant simply say fire once every 100MS as I dont get a responsive single press . I cant say fire once every 40MS as my repeated fire look to bunched up. I tried a single press < 40MS and repeated fire if hold the key down for >300MS

Share this post


Link to post
Share on other sites
This is actually quite simple if you use a "cooldown" variable where you fire the missiles in your update function.

The algorithm is like this:

void update(int milliseconds)
{
if (missile_fire_cooldown > 0)
missile_fire_cooldown -= milliseconds;
else if (fire_missile_button_is_pressed())
{
missile_fire_cooldown = missile_fire_interval;
fire_missile();
}

Share this post


Link to post
Share on other sites
Keep track of a fire delay time as long as your fire button is pressed to avoid rapid firing. Reset this delay when the button is released, so quickly tapping the button will allow for a higher rate of fire.

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