Skip to main content
added 1165 characters in body
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

You can add a little timer:

if(gc.getInput().isKeyDown(Input.KEY_SPACE) && 
 gameTime.getTimeMS() - shootTime > shootDelayInMilliseconds) {
   shootTime = gameTime.getTimeMS();
   bullets.add(new Bullet(player.getPos().getX() + 30,player.getPos().getY() +  17));  
}

Basically you are noting the last time your character shot, then ensuring that enough time has passed before shooting again.

Note thatAs for adding multiple bullets to the codearray list, it's doing this because you likely can't press the space bar fast enough to only have the space bar pressed for one frame. Each update you're adding one bullet. Add the delay like above will likely solve that problem. If you want to have a longer delay before you start repeating shots, you can doing something like:

if(gc.getInput().isKeyDown(Input.KEY_SPACE) && !previousInput.isKeyDown(Input.KEY_SPACE)) {
   startShootTime = gameTime.getTimeMS();
}
if(gc.getInput().isKeyDown(Input.KEY_SPACE) && 
 gameTime.getTimeMS() - shootTime > shootDelayInMilliseconds &&
 gameTime.getTimeMS() - startShootTime > initalShootDelayInMilliseconds ) {
   shootTime = gameTime.getTimeMS();
   bullets.add(new Bullet(player.getPos().getX() + 30,player.getPos().getY() +  17));  
}

Where initalShootDelayInMilliseconds is just javaish pseudo codethe time before holding the space bar causes repeated shots and shootDelayInMilliseconds is the time between each repeated shot thereafter. This is similar to the keyboard input for many text editors, holding a key for a certain amount of time will cause that key to repeat at a (often shorter) delay rate.

You can add a little timer:

if(gc.getInput().isKeyDown(Input.KEY_SPACE) && 
 gameTime.getTimeMS() - shootTime > shootDelayInMilliseconds) {
   shootTime = gameTime.getTimeMS();
   bullets.add(new Bullet(player.getPos().getX() + 30,player.getPos().getY() +  17));  
}

Basically you are noting the last time your character shot, then ensuring that enough time has passed before shooting again.

Note that the code above is just javaish pseudo code.

You can add a little timer:

if(gc.getInput().isKeyDown(Input.KEY_SPACE) && 
 gameTime.getTimeMS() - shootTime > shootDelayInMilliseconds) {
   shootTime = gameTime.getTimeMS();
   bullets.add(new Bullet(player.getPos().getX() + 30,player.getPos().getY() +  17));  
}

Basically you are noting the last time your character shot, then ensuring that enough time has passed before shooting again.

As for adding multiple bullets to the array list, it's doing this because you likely can't press the space bar fast enough to only have the space bar pressed for one frame. Each update you're adding one bullet. Add the delay like above will likely solve that problem. If you want to have a longer delay before you start repeating shots, you can doing something like:

if(gc.getInput().isKeyDown(Input.KEY_SPACE) && !previousInput.isKeyDown(Input.KEY_SPACE)) {
   startShootTime = gameTime.getTimeMS();
}
if(gc.getInput().isKeyDown(Input.KEY_SPACE) && 
 gameTime.getTimeMS() - shootTime > shootDelayInMilliseconds &&
 gameTime.getTimeMS() - startShootTime > initalShootDelayInMilliseconds ) {
   shootTime = gameTime.getTimeMS();
   bullets.add(new Bullet(player.getPos().getX() + 30,player.getPos().getY() +  17));  
}

Where initalShootDelayInMilliseconds is the time before holding the space bar causes repeated shots and shootDelayInMilliseconds is the time between each repeated shot thereafter. This is similar to the keyboard input for many text editors, holding a key for a certain amount of time will cause that key to repeat at a (often shorter) delay rate.

Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

You can add a little timer:

if(gc.getInput().isKeyDown(Input.KEY_SPACE) && 
 gameTime.getTimeMS() - shootTime > shootDelayInMilliseconds) {
   shootTime = gameTime.getTimeMS();
   bullets.add(new Bullet(player.getPos().getX() + 30,player.getPos().getY() +  17));  
}

Basically you are noting the last time your character shot, then ensuring that enough time has passed before shooting again.

Note that the code above is just javaish pseudo code.