How to make bullet shoot only when the previous bullet is off the screen

Hi everyone, Iā€™m having trouble with shooting another bullet only when the previous bullet fired is off the screen. This is my current code.

int x = 30;
int y = 60;
float gunX = x+20;
float gunY = y+20;
float circleY;
float circleX;
boolean bulletFlies=false;
float bulletX = 20;
float bulletY = 20;

void setup() {
size(500, 500);
circleY = random(25,480);
circleX = 450;
}

void draw() {
background(0);
fill(255, 56, 99);
ellipse(x, y, 60, 60);
gunX = x+12;
gunY = y;
bullet();

fill(255);
circle(circleX,circleY, 80);
circleX=circleX-0.5;

}

void keyPressed() {
if (key == ā€˜sā€™) y = y + 8;
else if (key == ā€˜wā€™) y = y - 8;
else if (key == ā€™ ') {
bulletFlies=true;
bulletX = gunX+14;
bulletY = gunY+4;
}
}

void bullet() {
if (bulletFlies) {
ellipse(bulletX, bulletY, 6, 6);
bulletX = bulletX+5;
if (bulletX>width+12) {
bulletFlies=false;
}else if (key == ā€™ ā€™ && !bulletFlies){
}
}
}

this would be enough:

just check if the bulletFlies and if so, leave

  else if (key == ' ') {
    if (bulletFlies)
      return; // leave
    bulletFlies=true;
    bulletX = gunX+14;
    bulletY = gunY+4;
  }

okay thank you, I will try this

1 Like