• Advertisement
Sign in to follow this  

single fire and repeated fire

This topic is 3581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement