Boolean if else question

I have 4 buttons and on the last button I need the button to show bird when day is true and witch when night is true. If look at the bottom of the code block you can see my if else. What am I doing wrong?

///////night button x,y,w,h//////
int nightButtonX;
int nightButtonY;
int nightButtonW;
int nightButtonH;
///////day button x,y,w,h//////
int dayButtonX;
int dayButtonY;
int dayButtonW;
int dayButtonH; 
///////quit button x,y,w,h//////
int quitButtonX;
int quitButtonY;
int quitButtonW;
int quitButtonH; 
///////bird witchButton //////
int bwButtonX;
int bwButtonY;
int bwButtonW;
int bwButtonH;

//////// booleans for button to check to & draw//// 
boolean night = false;
boolean day = true;



void setup() {
  size(600,500);
}


void draw() {
  //onload of sketch check if day is true
  if (day) {
    dayBackground();
  }
  //call night button
  nightButton(nightButtonX,nightButtonY,nightButtonW,nightButtonH);
  //call day button
  dayButton(dayButtonX,dayButtonY,dayButtonW,dayButtonH);
  //call quit button
  quitButton(quitButtonX,quitButtonY,quitButtonW,quitButtonH);
  //call bird/witch button
  bwButton(bwButtonX,bwButtonY,bwButtonW,bwButtonH);

}

void dayBackground(){
  //day Scene
  background(255);
}

void nightBackground(){
  //night Scene
  background(0);
}

void nightButton(int nightButtonX,int nightButtonY,int nightButtonW,int nightButtonH){ 
  //button body and postion variables set
  nightButtonX = 40;
  nightButtonY = 400;
  nightButtonW = 100;
  nightButtonH = 25;
  fill(175);
  rect(nightButtonX,nightButtonY,nightButtonW,nightButtonH);
  fill(0);
  text ("night", nightButtonX+25, nightButtonY+15);
   //check if mouse pressed and if mouseX & mouseY are in these buttons if they are set boolean and run funtion
   if (mouseX > nightButtonX && mouseX < nightButtonX+nightButtonW && mouseY > nightButtonY && mouseY < nightButtonY+nightButtonH && mousePressed) {
    day = false;
    nightBackground();
   }
}

void dayButton(int dayButtonX,int dayButtonY,int dayButtonW,int dayButtonH){ 
  //button body and postion variables set
  dayButtonX = 175;
  dayButtonY = 400;
  dayButtonW = 100;
  dayButtonH = 25;
  fill(175);
  rect(dayButtonX,dayButtonY,dayButtonW,dayButtonH);
  fill(0);
  text ("day", dayButtonX+25, dayButtonY+15);
  //check if mouse pressed and if mouseX & mouseY are in these buttons if they are 
  if (mouseX > dayButtonX && mouseX < dayButtonX+dayButtonW && mouseY > dayButtonY && mouseY < dayButtonY+dayButtonH && mousePressed) {
    night = true;
    dayBackground();
   }
}

void quitButton(int quitButtonX,int quitButtonY,int quitButtonW,int quitButtonH){ 
  //button body and postion variables set
  quitButtonX = 300;
  quitButtonY = 400;
  quitButtonW = 100;
  quitButtonH = 25;
  fill(175);
  rect(quitButtonX,quitButtonY,quitButtonW,quitButtonH);
  fill(0);
  text ("quit", quitButtonX+25, quitButtonY+15);
  //check if mouse pressed and if mouseX & mouseY are in these buttons if they are 
  if (mouseX > quitButtonX && mouseX < quitButtonX+quitButtonW && mouseY > quitButtonY && mouseY < quitButtonY+quitButtonH && mousePressed) {
    exit();
   }
}

void bwButton(int bwButtonX,int bwButtonY,int bwButtonW,int bwButtonH){ 
  //button body and postion variables set
  bwButtonX = 425;
  bwButtonY = 400;
  bwButtonW = 100;
  bwButtonH = 25;
  fill(175);
  rect(bwButtonX,bwButtonY,bwButtonW,bwButtonH);
  fill(0);
  //check if day or night replace text on button
   if (day == true && night == false) {
      text ("bird", bwButtonX+25, bwButtonY+15);
   }else if(night == true && day == false){
      text ("witch", bwButtonX+25, bwButtonY+15);
   }


}
Tagged:

Answers

  • Answer ✓

    The problem is that it is possible for day and might variables to be both true or both false so it won't trigger either condition.

    Change line 71 to

    day = false; night = true;

    and line 88 to

    night = true; day = false;

    BUT since

    when day == true then night == false

    when night == true then day == false

    You actually only need one boolean e.g. day when it is true then it is daytime if it is false then it is nighttime. So I suggest you get rid of the 'night' boolean and just use the one boolean it will simplify your if statements.

  • Many blessings on you. Thank you very much, I took all that crap out and now it works.

  • edited November 2014

    also separate displaying buttons and checking them pls

    • check buttons from func mousePressed() - much better than boolean mousepressed as you have now

    reason: the func gets only called once per click, your boolean a few times...

    thus clicks are more stable

Sign In or Register to comment.