If statement only works once


#1

Help


on line 167 i have an if statement for a game where blocks fall and you have to dodge them so im making the lives system but for some reason it only detects once that you have hit a block and any other time you hit it ,it won't detect, pls help me look at line 167 and when testing look for a fast RED block,
P.S Im using processing and look for a line with " TEST !!!" which is line 167


int spawnTimeStart1 = 0;
int spawnTimeStart2 = 0;
int spawnTimeStart3= 0;
int spawnTimeStart4 = 0;
int spawnTimeStart5 = 0;
int spawnTimeStart6= 0;  
int spawnTimeStart7 = 0;
int spawnTimeStart8 = 0;
int spawnTimeStart9 = 0;

boolean spawnTimeSwch1 = true;
boolean spawnTimeSwch2 = true;
boolean spawnTimeSwch3 = true;
boolean spawnTimeSwch4 = true;
boolean spawnTimeSwch5 = true;
boolean spawnTimeSwch6 = true;
boolean spawnTimeSwch7 = true;
boolean spawnTimeSwch8 = true;
boolean spawnTimeSwch9 = true;

int blockY1 = displayHeight;
int blockY2 = displayHeight;
int blockY3 = displayHeight;
int blockY4 = displayHeight;
int blockY5 = displayHeight;
int blockY6 = displayHeight;
int blockY7 = displayHeight;
int blockY8 = displayHeight;
int blockY9 = displayHeight;

float blockX1 = displayWidth;
float blockX2 = displayWidth;
float blockX3 = displayWidth;
float blockX4 = displayWidth;
float blockX5 = displayWidth;
float blockX6 = displayWidth;
float blockX7 = displayWidth;
float blockX8 = displayWidth;
float blockX9 = displayWidth;

int startTimeMs;
// The time until the game starts, in milliseconds
// (easy to convert to seconds, sec = ms/1000)
final int startDelayMs = 5000;
boolean atStartup = true;



float rectx = 100;
float recty= 30;

float rectx1 = 200;
float recty1= 40;

float rectx2 = 350;
float recty2= 60;

float rectx3 = 450;
float recty3= 75;


int score = 0;

int lives = 3;

int time;

float playerX;
float playerY;




void setup() {
  fullScreen();
  //size(700, 700);
  playerX = int(displayWidth/2);
  playerY = int(displayHeight * 0.9);


  rectx = int(displayWidth*0.25);

  rectx1 = int(displayWidth*0.65);

  rectx2 = int(displayWidth*0.45);

  rectx3 = int(displayWidth*0.75);
}


void draw() {
// println(playerY);
 
  background(240);
  rectMode(CENTER);
  fill(0,255, 0); 
  rect(rectx, recty, 25, 25); 
  fill(255,0,0);
  rect(rectx1, recty1, 25, 25);
  rect(rectx2, recty2, 25, 25);
  rect(rectx3, recty3, 25, 25);
  fill(255);
  // If we're in the startup time window, show a countdown
  if (atStartup) {
    // The current time, in milliseconds
    int curTimeMs = millis();
    // The remaining time in the startup period
    int startupTimeRemainingMs = startDelayMs - (curTimeMs - startTimeMs);
    startScreen(startupTimeRemainingMs);
    atStartup = startupTimeRemainingMs > 0;
    // Short-circuit if we're still in the startup phase.
    return;
  }

  fill(0);
  textAlign(CENTER, CENTER);
  text("GO!", displayWidth/2, displayHeight/2);
  fill(255, 0, 0);
  block1( 4, 200, 0, displayWidth -40, 25, 25);
  fill(0);
  block2(4, 150, 0, displayWidth -40, 25, 25);
  block3(4, 20, 0, displayWidth -40, 25, 25);
  block4(4, 35, 0, displayWidth -40, 25, 25);
  block5(4, 50, 0, displayWidth -40, 25, 25);
  block6(4, 150, 0, displayWidth -40, 25, 25);
  block7(4, 250, 0, displayWidth -40, 25, 25);
  block8(4, 115, 0, displayWidth -40, 25, 25);
  block9(4, 2, 0, displayWidth -40, 25, 25);
  fill(0);
  ellipse(playerX, playerY, 25, 25);

  recty  += 2;
  recty1 += 2;
  recty2 += 2;
  recty3 += 2;




  if (recty >displayHeight  || recty1>displayHeight  || recty2>displayHeight  || recty3>displayHeight   )  // red squares
  {
    recty  = 30;     
    recty1 = 40;
    recty2 = 60;
    recty3 = 80;
  }



  // faster red squares

  if (  playerX - rectx +25 >= 0 &&  playerX - rectx -25<=25 && playerY - recty ==0  ) {
    println("game over 1");
    lives =lives -1;
  }   if (  playerX - rectx1 +25 >= 0 &&  playerX - rectx1 -25<= 25 && playerY - recty1 ==0 ) {
    println("game over 1");
    lives =lives -1;
  }   
  if (  playerX - rectx2 +25 >= 0 &&  playerX - rectx2 -25<= 25 && playerY - recty2 ==0  ) {
    println("game over 1");
    lives =lives -1;
  } 
  if (  playerX - rectx3 +25 >= 0 &&  playerX - rectx3 -25<= 25 && playerY - recty3 ==0 ) {
    println("game over 1");
    lives =lives -1;
  } 
  if (playerX - blockX1+55 >= 0 &&  playerX - blockX1 -55<= 55 && playerY == blockY1     ) {     // TEST!!!!!!!!!!
    println("game over test");
    lives =lives -1;
    println(playerX);
    println(blockX1);
    
  } 
  if (  playerX - blockX2+25 >= 0 &&  playerX - blockX2 -25<= 25 && playerY - blockY2 ==0 ) {
    println("game over");
    lives =lives -1;
  } 
  if (  playerX - blockX3+25 >= 0 &&  playerX - blockX3 -25<= 25 && playerY - blockY3 ==0 ) {
    println("game over");
    lives =lives -1;
  }   
  if (  playerX - blockX4+25 >= 0 &&  playerX - blockX4 -25<=25 && playerY - blockY4 ==0 ) {
    println("game over");
    lives =lives -1;
  } 
  if (  playerX - blockX5+25 >= 0 &&  playerX - blockX5 -25<= 25 && playerY - blockY5 ==0  ) {
    println("game over");
    lives =lives -1;
  } 
  if (  playerX - blockX6 +25 >= 0 &&  playerX - blockX6 -25<=25 && playerY - blockX6 ==0  ) {
    println("game over ");
    lives =lives -1;
  }   
  if (  playerX - blockX7 +25 >= 0 &&  playerX - blockX7 -25<= 25 && playerY - blockX7 ==0 ) {
    println("game over ");
    lives =lives -1;
  }   
  if (  playerX - blockX8 +25 >= 0 &&  playerX - blockX8 -25<= 25 && playerY - blockX8 ==0  ) {
    println("game over ");
    lives =lives -1;
  } 
  if (  playerX - blockX9 +25 >= 0 &&  playerX - blockX9 -25<= 25 && playerY - blockX9 ==0  ) {
    println("game over ");
    lives =lives -1;
  }
  score = millis()/1000-5 ;
  textSize(50);
  text(score, 40, 30);
  textSize(40);
  text("lives:"+ lives, displayWidth- 100, 30 );


  if (lives==0) {
    background(255, 0, 0);
    textSize(60);
    text("GAME OVER", displayWidth/2, displayHeight/2);
    setup();
    score+=0;
  }
}



void block1(int blockYDropSpeed1, int spawnTimeRequirement1, int blockXmin1, int blockXmax1, int blockWidth1, int blockHeight1) {
  if (spawnTimeSwch1 == true) {
    spawnTimeStart1++;
  }

  if (spawnTimeRequirement1 == spawnTimeStart1 && spawnTimeSwch1 == true) {
    spawnTimeStart1 = 0;
    spawnTimeSwch1 = false;
    blockX1 = int(random(blockXmin1, blockXmax1));
  }

  if (spawnTimeSwch1 == false) {
    blockY1 += blockYDropSpeed1;
  }
  if (blockY1 > displayWidth && spawnTimeSwch1 == false) {
    spawnTimeSwch1 = true;
    blockY1 = -25;
    blockX1 = -25;
  }
  rect(blockX1, blockY1, blockWidth1, blockHeight1);
}


void block2(int blockYDropSpeed2, int spawnTimeRequirement2, int blockXmin2, int blockXmax2, int blockWidth2, int blockHeight2) {
  if (spawnTimeSwch2 == true) {
    spawnTimeStart2++;
  }

  if (spawnTimeRequirement2 == spawnTimeStart2 && spawnTimeSwch2 == true) {
    spawnTimeStart2 = 0;
    spawnTimeSwch2 = false;
    blockX2 =int( random(blockXmin2, blockXmax2));
  }

  if (spawnTimeSwch2 == false) {
    blockY2 += blockYDropSpeed2;
  }
  if (blockY2 > displayWidth && spawnTimeSwch2 == false) {
    spawnTimeSwch2 = true;
    blockY2 = -25;
    blockX2 = -25;
  }

  rect(blockX2, blockY2, blockWidth2, blockHeight2);
}


void block3(int blockYDropSpeed3, int spawnTimeRequirement3, int blockXmin3, int blockXmax3, int blockWidth3, int blockHeight3) {
  if (spawnTimeSwch3 == true) {
    spawnTimeStart3++;
  }

  if (spawnTimeRequirement3 == spawnTimeStart3 && spawnTimeSwch3 == true) {
    spawnTimeStart3 = 0;
    spawnTimeSwch3 = false;
    blockX3 = int(random(blockXmin3, blockXmax3));
  }

  if (spawnTimeSwch3 == false) {
    blockY3 += blockYDropSpeed3;
  }
  if (blockY3 > displayWidth && spawnTimeSwch3 == false) {
    spawnTimeSwch3 = true;
    blockY3 = -25;
    blockX3 = -25;
  }

  rect(blockX3, blockY3, blockWidth3, blockHeight3);
}


void block4(int blockYDropSpeed4, int spawnTimeRequirement4, int blockXmin4, int blockXmax4, int blockWidth4, int blockHeight4) {
  if (spawnTimeSwch4 == true) {
    spawnTimeStart4++;
  }

  if (spawnTimeRequirement4 == spawnTimeStart4 && spawnTimeSwch4 == true) {
    spawnTimeStart4 = 0;
    spawnTimeSwch4 = false;
    blockX4 = int(random(blockXmin4, blockXmax4));
  }

  if (spawnTimeSwch4 == false) {
    blockY4 += blockYDropSpeed4;
  }
  if (blockY4 > displayWidth && spawnTimeSwch4 == false) {
    spawnTimeSwch4 = true;
    blockY4 = -25;
    blockX4 = -25;
  }
  rect(blockX4, blockY4, blockWidth4, blockHeight4);
}


void block5(int blockYDropSpeed5, int spawnTimeRequirement5, int blockXmin5, int blockXmax5, int blockWidth5, int blockHeight5) {
  if (spawnTimeSwch5 == true) {
    spawnTimeStart5++;
  }

  if (spawnTimeRequirement5 == spawnTimeStart5 && spawnTimeSwch5 == true) {
    spawnTimeStart5 = 0;
    spawnTimeSwch5 = false;
    blockX5 = int(random(blockXmin5, blockXmax5));
  }

  if (spawnTimeSwch5 == false) {
    blockY5 += blockYDropSpeed5;
  }
  if (blockY5 > displayWidth && spawnTimeSwch5 == false) {
    spawnTimeSwch5 = true;
    blockY5 = -25;
    blockX5 = -25;
  }
  rect(blockX5, blockY5, blockWidth5, blockHeight5);
}


void block6(int blockYDropSpeed6, int spawnTimeRequirement6, int blockXmin6, int blockXmax6, int blockWidth6, int blockHeight6) {
  if (spawnTimeSwch6 == true) {
    spawnTimeStart6++;
  }

  if (spawnTimeRequirement6 == spawnTimeStart6 && spawnTimeSwch6 == true) {
    spawnTimeStart6 = 0;
    spawnTimeSwch6 = false;
    blockX6 = int(random(blockXmin6, blockXmax6));
  }

  if (spawnTimeSwch6 == false) {
    blockY6 += blockYDropSpeed6;
  }
  if (blockY6 > displayWidth && spawnTimeSwch6 == false) {
    spawnTimeSwch6 = true;
    blockY6 = -25;
    blockX6 = -25;
  }

  rect(blockX6, blockY6, blockWidth6, blockHeight6);
}

void block7(int blockYDropSpeed7, int spawnTimeRequirement7, int blockXmin7, int blockXmax7, int blockWidth7, int blockHeight7) {
  if (spawnTimeSwch7 == true) {
    spawnTimeStart7++;
  }

  if (spawnTimeRequirement7 == spawnTimeStart7 && spawnTimeSwch7 == true) {
    spawnTimeStart7 = 0;
    spawnTimeSwch7 = false;
    blockX7 = int(random(blockXmin7, blockXmax7));
  }

  if (spawnTimeSwch7 == false) {
    blockY7 += blockYDropSpeed7;
  }
  if (blockY7 > displayWidth && spawnTimeSwch7 == false) {
    spawnTimeSwch7 = true;
    blockY7 = -25;
    blockX7 = -25;
  }

  rect(blockX7, blockY7, blockWidth7, blockHeight7);
}

void block8(int blockYDropSpeed8, int spawnTimeRequirement8, int blockXmin8, int blockXmax8, int blockWidth8, int blockHeight8) {
  if (spawnTimeSwch8 == true) {
    spawnTimeStart8++;
  }

  if (spawnTimeRequirement8 == spawnTimeStart8 && spawnTimeSwch8 == true) {
    spawnTimeStart8 = 0;
    spawnTimeSwch8 = false;
    blockX8 = int(random(blockXmin8, blockXmax8));
  }

  if (spawnTimeSwch8 == false) {
    blockY8 += blockYDropSpeed8;
  }
  if (blockY8 > displayWidth && spawnTimeSwch8 == false) {
    spawnTimeSwch8 = true;
    blockY8 = -25;
    blockX8 = -25;
  }

  rect(blockX8, blockY8, blockWidth8, blockHeight8);
}

void block9(int blockYDropSpeed9, int spawnTimeRequirement9, int blockXmin9, int blockXmax9, int blockWidth9, int blockHeight9) {
  if (spawnTimeSwch9 == true) {
    spawnTimeStart9++;
  }

  if (spawnTimeRequirement9 == spawnTimeStart9 && spawnTimeSwch9 == true) {
    spawnTimeStart9 = 0;
    spawnTimeSwch9 = false;
    blockX9 = int(random(blockXmin9, blockXmax9));
  }

  if (spawnTimeSwch9 == false) {
    blockY9 += blockYDropSpeed9;
  }
  if (blockY9 > displayWidth && spawnTimeSwch9 == false) {
    spawnTimeSwch9 = true;
    blockY9 = -25;
    blockX9 = -25;
  }

  rect(blockX9, blockY9, blockWidth9, blockHeight9);
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == LEFT) {
      playerX= playerX-10;
    } else if (keyCode == RIGHT) {
      playerX = playerX +10;
    }
  }
}


void startScreen(int remainingTimeMs) {
  background(39, 152, 25);
  textSize(100);
  fill(0);
  textAlign(CENTER, CENTER);
  // Show the remaining time, in seconds;
  // show n when there are n or fewer seconds remaining. 
  text(ceil(remainingTimeMs/1000.0), displayWidth/2, displayHeight/2);
}


#2

After if, you use else if.

function example() {
  var a = prompt("How are you?");
  if (a === "good") {
  alert("That's good");
  }
  else if (a === "bad") {
  alert("too bad :(");
  }
}

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.