简体   繁体   English

更改子弹离开屏幕上的值

[英]Changing value on bullet leaving screen

I have this bunch of badly written code in Processing 3.5.3.我在处理 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;
}

You had some dead code (code that is never run) in your draw loop.您的draw循环中有一些死代码(从不运行的代码)。 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).如果bultr为 1,则执行 (a),否则执行 (b)。
    • (a) Execute drone1(...) , and set bultr to that output. (a) 执行drone1(...) ,并将bultr设置为output。
    • (b) Set bulletmover to 0 and bultr to 1. (b) 将bulletmover设置为 0,将bultr设置为 1。
  2. Proceed to the code after the if...else statement.继续执行if...else语句之后的代码。

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).所以如果你在它之前直接int bultr = 1 ,计算机总是会看到bultr是 1,所以它总是做 (a) 而从不做 (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.如果bultr为 1,则执行drone1(...) ,并将bultr设置为 output。
  2. If bultr is not 1, set bulletmover to 0 and bultr to 1.如果bultr不是 1,则将bulletmover设置为 0,将bultr设置为 1。
  3. Proceed to the code after the if...else statement.继续执行if...else语句之后的代码。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM