-4

Currently I have the code create a bullet 60 times per second (setInterval) I have tried for loops but the did not work at all. Does any one have an idea how can I regulate the fire rate?

Thanks.

Eden
  • 9
  • 1
  • 2
    Welcome to [so]! At this site you are expected to try to **write the code yourself**. After **[doing more research](//meta.stackoverflow.com/questions/261592)** if you have a problem you can **post what you've tried** with a **clear explanation of what isn't working** and providing a [**Minimal, Complete, and Verifiable example**](//stackoverflow.com/help/mcve). I suggest reading [ask] a good question and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Also, be sure to take the [tour] and read **[this](//meta.stackoverflow.com/questions/347937/)**. – Alon Eitan May 13 '17 at 20:55
  • 1
    You already [asked](http://stackoverflow.com/questions/43760692/html5-canvas-game-how-to-make-players-shoot) a low quality question without a code, SHOW us your code – Alon Eitan May 13 '17 at 20:56
  • 1
    @AlonEitan What makes a question like this get so many votes to be closed while a question like this http://stackoverflow.com/q/2142535/3877726 gets protected? Seams to me its just the luck of the draw what makes a good question. – Blindman67 May 13 '17 at 23:05
  • @Blindman67 Sorry for the late reply, but that question is more than 7 years old. Times have changed, and asking that same question today will get you downvoted and closed too. I think the upvotes comes from this question is being high ranked on Google searches. I did like your answer though! – Alon Eitan May 14 '17 at 14:18
  • @Blindman67 And about the question being protected - Read [this comment](https://meta.stackoverflow.com/questions/349170/an-ancient-popular-question-has-been-hijacked-what-can-we-do-now#comment472910_349170) for a possible reason – Alon Eitan May 14 '17 at 14:21
  • @AlonEitan whats the point of showing the code? i didnt know how to shoot so how it would help to know how players are moving or so? – Eden May 24 '17 at 11:33
  • Show the work you have done so the community has something to help you with rather than doing it for you. – Jesse Johnson Jun 03 '17 at 03:06

1 Answers1

2

In most real time games you would have a main loop that you control your animations from. You would add the fire control to this.

// object to hold details about the gun
const gun = {
    fireRate : 2,  // in frames (if 60 frames a second 2 would be 30 times a second
    nextShotIn : 0, // count down timer till next shot
    update() {  // call every frame
        if(this.nextShotIn > 0){
            this.nextShotIn -= 1;   
        }
    }, 
    fire(){
        if(this.nextShotIn === 0){
            // call function to fire a bullet
            this.nextShotIn = this.fireRate; // set the countdown timer
         }
    }
}


function mainAnimationLoop()
     // game code


     gun.update();
     if(fireButtonDown){
         gun.fire(); // fire the gun. Will only fire at the max rate you set with fireRate
     }


     // rest of game code
}
Blindman67
  • 51,134
  • 11
  • 73
  • 136