mouseClicked 'if' statements only run through once. How do I change this?

edited June 2014 in How To...

Only the "zombies" part works right now (top left box on homescreen). Works well, I can click through in the way I want to, but once I get redirected to the homescreen again the buttons that previously worked no longer do. (To do this: click on top left box, click on the "axe" and then click on the white box. This will bring you to all the states.)

float x;
float y;
boolean zombiesOver = false;
boolean nukeOver = false;
boolean pandemicOver = false;
boolean sunOver = false;
int screen = 0;
float zx[];
float zy[];
int numberZombies;
float ax, ay;
float zxSpeed[], zySpeed[];
boolean axeOver = false;
boolean deadOver = false;
boolean on = true;

void setup() {
  size (600,600);
  numberZombies = 10;
  zx = new float[numberZombies]; zy = new float[numberZombies];
  zxSpeed = new float[numberZombies];
  zySpeed = new float[numberZombies];
  for (int n=0; n<numberZombies; n++) {
    zx[n]= random(30,550);
    zy[n]= random(50,500);
    zxSpeed[n] = random(-0.6,0.6);
    zySpeed[n] = random(-0.6,0.6);
  }
   ax= random(200,400);
   ay= random(200,400);
}

void draw() {

  if (screen == 0) {
  homeScreen();
  } if (screen == 1) {
    zombiesScreen();
  } if (screen == 2) {
    nukeScreen();
  } if (screen == 3) {
    pandemicScreen();
  } if (screen == 4) {
    sunScreen();        //lol, sunscreen.
  } if (screen == 5) {
    deadScreen();
  }

}


void homeScreen() { //Home Screen function
  zombiesButton();
  nukeButton();
  pandemicButton();
  sunButton();
background (0);
textSize(50);
fill(255);
text("Survive.", 213, 100);
stroke(255);
strokeWeight(3);
fill(153,76,0);
rect(80,170,150,100);
fill(112,110,107);
rect(370,170,150,100);
fill(0,196,59);
rect(80,370,150,100);
fill(255,162,0);
rect(370,370,150,100);
textSize(10);
fill(255);
text("Mouse for movement. Click for action.", 6, 594);
hero(mouseX,mouseY);
}




void nukeScreen() {
  hills();
  hero(mouseX,440);
}
void sunScreen() {
  hills();
  hero(mouseX,440);
}




void hills() { //for nukes and sun
  background(135,206,250);
  noStroke();
  fill(23,122,11);
  ellipse(80,280,200,150);
  ellipse(300,310,260,220);
  ellipse(600,370,400,400);

  fill(40,180,20);
  ellipse(0,340,260,270);
  ellipse(240,360,300,260);
  ellipse(450,365,250,150);

  fill(30,215,7);
  ellipse(55,450,250,250);
  ellipse(280,460,300,200);
  ellipse(575,480,360,315);

  fill(255,165,0);
  ellipse(25,0,200,200);

  fill(26,255,0);
  rect(0,500,600,100);
}

void pandemicScreen() {
background (112,110,107);
}

void zombiesScreen() {
  axeButton();
  background (68,42,2);
  axe(ax,ay);
  hero(mouseX,mouseY);
  for(int n=0; n<numberZombies; n++) {
    zy[n] = zy[n] + zySpeed[n];
    if(zy[n] > 600) { zySpeed[n] = zySpeed[n] * -1; }
    if(zy[n] < 0) { zySpeed[n] = zySpeed[n] * -1; }
    zx[n] = zx[n] + zxSpeed[n];
    if(zx[n] > 600) { zxSpeed[n] = zxSpeed[n] * -1; }
    if(zx[n] < 0) { zxSpeed[n] = zxSpeed[n] * -1; }
  }
  for(int n=0; n<numberZombies; n++) {
    zombie(zx[n],zy[n]);
  }
}

void zombie(float zx, float zy) {
  noStroke();
  fill(56,142,6); 
  rect(zx-11,zy-23,22,15);
  rect(zx-20,zy,40,30);//arms
  rect(zx-11,zy-18,22,18); //face
  rect(zx-13,zy,26,60);  //feet
  fill(104,78,7);    //legs
  rect(zx-13,zy,26,53);
  fill(97,93,84);    //torso
  rect(zx-13,zy,26,27);
  fill(255); //eyes
  rect(zx-7,zy-14,4,4);
  rect(zx+3,zy-14,4,4);
}

void axe(float ax, float ay) {
  noStroke();
  fill(150);
  triangle(ax-5,ay-2,ax-2,ay+5,ax,ay);
  stroke(230,141,6);
  strokeWeight(3.5);
  line(ax,ay,ax+14,ay+28);
  noStroke();
  fill(150);
  triangle(ax+5,ay-15,ax+15,ay+5,ax,ay);
  stroke(230);
  strokeWeight(2);
  line(ax+5,ay-14,ax+15,ay+4);
}

void hero(float hx, float hy) {
  noStroke();
  fill(255,255,51); //hair
  rect(hx-11,hy-23,22,15);
  fill(255,204,153); //arms
  rect(hx-20,hy,40,30);
  rect(hx-11,hy-18,22,18); //face
  fill(102,51,0);     //feet
  rect(hx-13,hy,26,60);
  fill(0,0,255);    //legs
  rect(hx-13,hy,26,53);
  fill(255,0,0);    //torso
  rect(hx-13,hy,26,27);
  fill(82,204,255); //eyes
  rect(hx-7,hy-14,4,4);
  rect(hx+3,hy-14,4,4);
}


void axeButton() {
if (mouseX > ax-2 && mouseX < ax+15
  && mouseY > ay-14 && mouseY < ay+28) {
    axeOver = true;
  } else {
      axeOver = false;
    }
}





//buttons activation
void mouseClicked() { //when buttons are pressed
directions();
}  








//buttons
void zombiesButton() {
  if (mouseX > 80 && mouseX < 230
  && mouseY >170 && mouseY <270) {
    zombiesOver = true;
  } else {
      zombiesOver = false;
    }
}

void nukeButton() {
   if (mouseX > 370 && mouseX < 520
  && mouseY >170 && mouseY <270) {
    nukeOver = true;
  } else {
      nukeOver = false;
    }
}

void pandemicButton() {
   if (mouseX > 80 && mouseX < 230
  && mouseY >370 && mouseY <470) {
    pandemicOver = true;
  } else {
      pandemicOver = false;
    }
}

void sunButton() {
   if (mouseX > 370 && mouseX < 520
  && mouseY >370 && mouseY <470) {
    sunOver = true;
  } else {
      sunOver = false;
    }
}

void deadButton() {
   if (mouseX > 200 && mouseX < 400
  && mouseY >200 && mouseY <400) {
    deadOver = true;
  } else {
      deadOver = false;
    }
}

void deadScreen() {
  deadButton();
  background(0);
  fill(255);
  rect(200,200,200,200);
}

void directions() {
  if (zombiesOver) {
        screen = 1;
      } if (nukeOver) {
        screen = 2;
      } if (pandemicOver) {
        screen = 3;
      } if (sunOver) {
        screen = 4;
      } if (axeOver) {
        screen = 5;
      } if (deadOver) {
        screen = 0;
      }
}

Answers

  • _vk_vk
    edited June 2014 Answer ✓

    I think the logic, the way it is, is hard to follow. Perhaps you could look into "states" (search), and also switch(). Instead of all those booleans and ifs you would have a gameState int indicating what to do. Maybe easier to keep track of everything. Like the problem you are having. You did not returned the booleans to its original states, so they don't behave as you wish. It easy to get lost. Without really digging into your code, i guessed this and just give it a shot, and... worked. But I really would try another logic in your place. : )

    try this directions instead:

    void directions() {
      if (zombiesOver) {
            screen = 1;
            zombiesOver = false;//<-
          } if (nukeOver) {
            screen = 2;
            nukeOver = false;//<-
          } if (pandemicOver) {
            screen = 3;
            pandemicOver = false;//<-
          } if (sunOver) {
            screen = 4;
            sunOver = false;//<-
          } if (axeOver) {
            screen = 5;
            axeOver = false;//<-
          } if (deadOver) {
            screen = 0;
            deadOver = false;//<-
          }
    }
    
  • THANK YOU SO MUCH.

    That was a very clear, very helpful answer. I super appreciate it. I hope that your weekend is as lovely as you have been.

Sign In or Register to comment.