My for loop does not work, why?

So i have made this for loop for my game but i does not work, and i have no idea why.

for (boolean Blue=true ;Blue=false;) {
  image(BackGround,0,0);
  BlueY--;
  image(BlueCar,282,BlueY);
  println("Blue For");
  if(BlueY==600){
  BlueY=0;
  Spawn=true;
  Score++;
  Blue=false;
}
}

The rest of the code is here if you need it

int BorderY=298,BorderX=312,BorderX2=-5,BorderY2=550,Score;
float PX,PY,CarColor,BlueX,BlueY=0,GreenX,GreenY=0,PurpleX,PurpleY=0,GreyX,GreyY=0,GameX,GameY;
PImage BackGround,GreyCar,PurpleCar,GreenCar,BlueCar,Player,myAnimation;
boolean Spawn=true,Purple=false,Blue=false,Green=false,Grey=false;

void setup() {
  size(420,600);
  BackGround=loadImage("Road.png"); //600,348
  GreyCar=loadImage("GreyCar.png");// 719,355
  PurpleCar=loadImage("PurpleCar.png");// 719,355
  GreenCar=loadImage("GreenCar.png");// 719,355
  BlueCar=loadImage("BlueCar.png");// 719,355
  Player=loadImage("Player.png");//50,38
  noCursor();

}

void draw() {
image(BackGround,0,0);
image(Player,PX,PY);
PX=mouseX-19;
PY=mouseY-25;
textSize(25);
text("Score",352,25);
textSize(40);
text(Score,373,65);
if (PY<=BorderY){
  PY=BorderY;
}
if (PY>=BorderY2){
  PY=BorderY2;
}
if (PX>=BorderX){
  PX=BorderX;
}
if (PX<=BorderX2){
  PX=BorderX2;
}
if (Spawn==true){
  int CarColor=int (random(1,5));
  println(CarColor);
  if (CarColor==1) {
    Grey=true;
    Spawn=false;
    println("Grey");
  }
  else if (CarColor==2) {
    Purple=true;
    Spawn=false;
    println("Purple");
  }
  else if (CarColor==3) {
    Green=true;
    Spawn=false;
    println("Green");
  }
  else if (CarColor==4) {
    Blue=true;
    Spawn=false;
    println("Blue");
  }  
}
for (boolean Blue=true ;Blue=false;) {
  image(BackGround,0,0);
  BlueY--;
  image(BlueCar,282,BlueY);
  println("Blue For");
  if(BlueY==600){
  BlueY=0;
  Spawn=true;
  Score++;
  Blue=false;
}
}

for (boolean Purple=true ;Purple=false;) {
  image(BackGround,0,0);
  PurpleY--;
  image(PurpleCar,102,PurpleY);
  println("Purple For");
  if(PurpleY==600){
    PurpleY=0;
    Spawn=true;
    Score++;
    Purple=false;
} 
}

for (boolean Green=true ;Green=false;) {
  image(BackGround,0,0);
  GreenY--;
  image(GreenCar,192,GreenY);
  println("Green For");
  if(GreenY==600){
    GreenY=0;
    Spawn=true;
    Score++;
    Green=false;
} 
}

for (boolean Grey=true ;Grey=false;) {
  image(BackGround,0,0);
  GreyY--;
  image(GreyCar,12,GreyY);
  println("Grey For");
  if(GreyY==600){
    GreyY=0;
    Spawn=true;
    Score++;
    Grey=false;
} 
}
}
1 Like

The type boolean is not really made for usage in a for-loop

for-loop is more for an int that you count up

for(int i=0; i < 10; i++) {

Example with boolean

This kind of works

(of course you can’t see an animation in an for-loop because of how draw() works)




void setup() {
  size(420, 600);
  frameRate(2); 
  noCursor();
  noLoop();
}

void draw() {

  for (boolean Blue=true; Blue==true||Blue==false; Blue=false) {

    println(Blue);

    if (Blue) {
      fill(255, 0, 0);
      rect(40, 40, 44, 44);
    } else {
      fill(0, 255, 0);
      rect(140, 40, 44, 44);
    }

    if (Blue==false) { 
      break;
    }
  }
}

3 Likes

It would be more elegant to have an int variable in the for loop that is from 0 to 1 and 0 signifies false and 1 true.