Search code examples
javaprocessing

Changing value on bullet leaving screen


I have this bunch of badly written code in Processing 3.5.3. I want the "drone" to shoot again when the "bullet" leaves the screen, but it starts glitching. Any ideas, what can be wrong, or how to improve the screen leaving detections or things like that. I copied here the whole code so you can try it out because it isn't so complicated yet

int charx;
int chary;
void setup() {
  charx = width/2;
  chary = height - height/10;
  fullScreen();
  background(0);
}
void keyPressed(){
  if(key=='w'){if(!(chary<30))chary-=15;}}
  if(key=='s'){if(!(chary>height-60)){chary+=15;}}
  if(key=='a'){charx-=15;if(charx<30){charx=width-30;}}
  if(key=='d'){charx+=15;if(charx>width-30){charx=30;}}
}
int bullet(int x, int y) {
  if ( y > height - 30){return(0);}
  fill(175, 0, 0);
  triangle(x, y, x-8, y-16, x+8, y-16);
  fill(255);
  return(1);
}
int launch = 0;
int starterx = 0;
int drone1(float startx, float xm, int y, int i) {
  int x = ceil(startx) + ceil(40 * sin(xm));
  if (i == 0) {
    starterx = ceil(startx);
  } else {
    if (launch == 0) {
      starterx = x;
      launch = 1;
      return(0);
    }}
  fill(0, 200, 50);
  rect(x, y, 30, 30);
  fill(255);
  if (launch == 1) {
    launch = bullet(ceil(asin(x)) + starterx + 15, y + 46 + i * 3);
  }
  return(1);}
void player(int x, int y) {
  fill(255);
  rect(x, y, 10, 10);
  fill(51, 51, 204);
  rect(x+10, y, 10, 10);
  rect(x-10, y, 10, 10);
  fill(255, 215, 0);
  rect(x, y-10, 10, 10);
  fill(255, 0, 0);
  rect(x+10, y+10, 10, 10);
  rect(x-10, y+10, 10, 10);
  fill(255);
}
int i = 0; 
int bulletmover = 0;
void draw() {
  background(0);
  player(charx, chary);
  int bultr = 1;
  if (bultr == 1) {
    bultr = drone1(width/2, 0.1 * i, i, bulletmover);
  } else {
    bulletmover = 0;
    bultr = 1;
  }
  bulletmover += 1;
  i += 1;
}

Solution

  • You had some dead code (code that is never run) in your draw loop. When you write

    if (bultr == 1) {
      bultr = drone1(width/2, 0.1 * i, i, bulletmover);
    } else {
      bulletmover = 0;
      bultr = 1;
    }
    

    the computer does this:

    1. Do one of the following. If bultr is 1, do (a), otherwise do (b).
      • (a) Execute drone1(...), and set bultr to that output.
      • (b) Set bulletmover to 0 and bultr to 1.
    2. Proceed to the code after the if...else statement.

    So if you put int bultr = 1 directly before that, the computer will always see that bultr is 1, so it will always do (a) and never (b). I didn't read your code all that thoroughly, but I think what you were trying to do is this:

    1. If bultr is 1, execute drone1(...), and set bultr to that output.
    2. If bultr is not 1, set bulletmover to 0 and bultr to 1.
    3. Proceed to the code after the if...else statement.

    In code, that would look like this:

    if (bultr == 1) {
      bultr = drone1(width/2, 0.1 * i, i, bulletmover);
    }
    if (bultr != 1) {
      bulletmover = 0;
      bultr = 1;
    }