1

I'm struggling on my code.org platform on how to make a delay, as I'm making a shooter game. When I click I want there to be some sort of reloading happening, so it could take me at least 3000ms to reload.

I heard some things about the timeOut() function, but I'm not sure how to use it. I tried many ways, like keeping the timeout outside the if, and inside the if.

Allan
  • 11
  • 2
  • Does this answer your question? [Simple debounce function in JavaScript](https://stackoverflow.com/questions/65303903/simple-debounce-function-in-javascript) – CodeThing Apr 14 '23 at 09:31
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Apr 14 '23 at 09:41

1 Answers1

0

You can use built-in setTimeOut function. As the docs says, it fires the function after the desired time ends. Here I build simple reload functionality for you, space to shoot, a to reload;

var bullets=6;
function draw() {
 
   if(bullets > 0) { 
     if (keyWentDown("space")){
      bullets=bullets-1
      console.log(bullets)
      }}
    if(bullets < 6){
      if(keyWentDown("a")){
        reload();
        
      }
    }  
      
}

function reload(){
  console.log('reloading')
  setTimeout(function() {
  console.log("relaoded");
  bullets = 6;
}, 3000);
}

Here's the Game Lab link; https://studio.code.org/projects/gamelab/ZyXRGRq5hyqcPXYTwcfAPfDihSHCyv1CesSnMBwjMWs

Cengiz
  • 162
  • 9