If else button issues

I am just a beginner with processing. I have coded a witch that floats on a broom. I have set it up so it you click on the witch and the stage should go from night to day. Can some one tell me what I did wrong? The if else statement I am having trouble with is on the bottom the code block.

float skyX=0;
float skyY=0;
float skyW=600;
float skyH=150;
float sunX=500;
float sunY=50;
float sunW=75;
float sunH=75;
float dx=1;
float moonX=250;
float moonY=70;
float moonW=75;
float moonH=75;
float witchx=590;
float witchy=50;
float witchw=60; 
float witchh=20;
int broomX =80;
int spacing =12;
int broomLen =40;
boolean click = false;




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


void draw() {
  background(3,255,80);
  stageNight(skyX,skyY,skyW,skyH);
}

///////// Day ///////////

void stageDay(float skyX, float skyY, float skyW, float skyH){

  // sky color and layout
  fill(192, 221, 250);  // Set fill to Light blue
  noStroke();
  rect(skyX, skyY, skyW, skyH); 

   //sun body
  strokeWeight(1);
  stroke(0);
  ellipseMode(CENTER);  // Set ellipseMode to CENTER
  fill(255, 243, 3);  // Set fill to yellow
  ellipse(sunX,sunY, sunW, sunH);  // Draw ellipse using CENTER mode

}

///////// night ///////////

void stageNight(float skyX, float skyY, float skyW, float skyH){
  // sky color and layout
  fill(3, 185, 255);  // Set fill to Light blue
  noStroke();
  rect(skyX, skyY, skyW, skyH); 

   //moon body
  strokeWeight(1);
  stroke(0);
  ellipseMode(CENTER);  // Set ellipseMode to CENTER
  fill(245, 220, 237);  // Set fill to yellow
  ellipse(moonX,moonY, moonW, moonH);  // Draw ellipse using CENTER mode

  witch(witchx, witchy, witchw, witchh);
  moveWitch();
}

///////// witch body///////////

void witch(float witchx, float witchy, float witchw, float witchh){
  //head
  strokeWeight(1);
  stroke(0);
  fill(255,0,0);
  ellipseMode(CENTER);  // Set ellipseMode to CENTER
  ellipse(witchx-48,witchy+20, witchw-20, witchh+20);  // Draw ellipse using CENTER mode

  //hat
  fill(0);  // Set to black
  noStroke();
  triangle( witchx-35,witchy, witchx+10-witchw, witchy-witchh-5, witchx-witchw, witchy-18+witchh);
  rect(witchx-80, witchy, 70, 10);  // Draw rect 

  //leg2
  stroke(70,191,19);  // Set to d green
  strokeWeight(10); 
  line(witchx-40, witchy+100, witchx-70, witchy+175);  

  //body
  fill(252,185,48);  // Set to brown
  noStroke();
  rect(witchx-85, witchy+41, 70, 90);  // Draw rect

  //broom
  stroke(185,131,13);  // Set to d green
  strokeWeight(10); 
  line(witchx-125, witchy+135, witchx+40, witchy+135);
  line(witchx+40, witchy+100, witchx+40, witchy+170);

  //broom lines for loop
  for (int broomY=150; broomY<=225; broomY+=spacing){
    strokeWeight(3);   
    line(witchx+40,broomY,witchx+40+broomLen,broomY);
  }

  //leg1
  stroke(70,191,19);  // Set to d green
  strokeWeight(10); 
  line(witchx-40, witchy+120, witchx-40, witchy+200);  

}

///////// move witch ///////////

void moveWitch(){
  // normal movement of the Witch From right to left
    if (witchx>width) witchx=0;{
      witchx = witchx-dx;//take dx and -1 untill 0 then run the next if 
    }
    if (witchx == -100) {
    witchx=600;// sets this back to 600 so witch can fly from the right
    }
}

///////// click witch ///////////

 void checkMouseClick () {
    if ( (mouseX > witchx) && (mouseX < witchx + witchw) && (mouseY > witchy) && (mouseY < witchy + witchw) ) {
      click = !click;
    }
 }

 void clicked () {
    if (click) {
    stageDay(skyX,skyY,skyW,skyH);
    }
    else {
    stageNight(skyX,skyY,skyW,skyH);
    }
  }
Tagged:

Answers

  • Answer ✓

    When are you imagining that your checkMouseClick() or click() functions are ever being called? Consider adding a mousePressed() function...

  • edited October 2014 Answer ✓

    this works better now

    issue was, you called stateNight in draw but you need to call clicked

    also your witch is drawn left from witchx mainly / partly

    and TfGuy44 was right of course

    float skyX=0;
    float skyY=0;
    float skyW=600;
    float skyH=150;
    float sunX=500;
    float sunY=50;
    float sunW=75;
    float sunH=75;
    float dx=1;
    float moonX=250;
    float moonY=70;
    float moonW=75;
    float moonH=75;
    float witchx=590;
    float witchy=50;
    float witchw=60;
    float witchh=20;
    int broomX =80;
    int spacing =12;
    int broomLen =40;
    
    boolean click = false;
    
    
    void setup() {
      size(600, 500);
    }
    
    
    void draw() {
      background(3, 255, 80);
      clicked();
      // stageNight(skyX, skyY, skyW, skyH);
    }
    
    ///////// Day ///////////
    
    void stageDay(float skyX, float skyY, float skyW, float skyH) {
    
      // sky color and layout
      fill(192, 221, 250);  // Set fill to Light blue
      noStroke();
      rect(skyX, skyY, skyW, skyH);
    
      //sun body
      strokeWeight(1);
      stroke(0);
      ellipseMode(CENTER);  // Set ellipseMode to CENTER
      fill(255, 243, 3);  // Set fill to yellow
      ellipse(sunX, sunY, sunW, sunH);  // Draw ellipse using CENTER mode
    }
    
    ///////// night ///////////
    
    void stageNight(float skyX, float skyY, float skyW, float skyH) {
      // sky color and layout
      fill(3, 185, 255);  // Set fill to Light blue
      noStroke();
      rect(skyX, skyY, skyW, skyH);
    
      //moon body
      strokeWeight(1);
      stroke(0);
      ellipseMode(CENTER);  // Set ellipseMode to CENTER
      fill(245, 220, 237);  // Set fill to yellow
      ellipse(moonX, moonY, moonW, moonH);  // Draw ellipse using CENTER mode
    
      witch(witchx, witchy, witchw, witchh);
      moveWitch();
    }
    
    ///////// witch body///////////
    
    void witch(float witchx, float witchy, 
    float witchw, float witchh) {
      //head
      strokeWeight(1);
      stroke(0);
      fill(255, 0, 0);
      ellipseMode(CENTER);  // Set ellipseMode to CENTER
      ellipse(witchx-48, witchy+20, witchw-20, witchh+20);  // Draw ellipse using CENTER mode
    
      //hat
      fill(0);  // Set to black
      noStroke();
      triangle( witchx-35, witchy, witchx+10-witchw, witchy-witchh-5, witchx-witchw, witchy-18+witchh);
      rect(witchx-80, witchy, 70, 10);  // Draw rect
    
      //leg2
      stroke(70, 191, 19);  // Set to d green
      strokeWeight(10);
      line(witchx-40, witchy+100, witchx-70, witchy+175); 
    
      //body
      fill(252, 185, 48);  // Set to brown
      noStroke();
      rect(witchx-85, witchy+41, 70, 90);  // Draw rect
    
      //broom
      stroke(185, 131, 13);  // Set to d green
      strokeWeight(10);
      line(witchx-125, witchy+135, witchx+40, witchy+135);
      line(witchx+40, witchy+100, witchx+40, witchy+170);
    
      //broom lines for loop
      for (int broomY=150; broomY<=225; broomY+=spacing) {
        strokeWeight(3);  
        line(witchx+40, broomY, witchx+40+broomLen, broomY);
      }
    
      //leg1
      stroke(70, 191, 19);  // Set to d green
      strokeWeight(10);
      line(witchx-40, witchy+120, witchx-40, witchy+200);
    }
    
    ///////// move witch ///////////
    
    
    void moveWitch() {
      // normal movement of the Witch From right to left
      if (witchx>width) witchx=0;
      {
        witchx = witchx-dx;//take dx and -1 untill 0 then run the next if
      }
      if (witchx <= -100) {
        witchx=600;// sets this back to 600 so witch can fly from the right
      }
    }
    
    ///////// click witch ///////////
    
    
    void mouseClicked () {
      if ( (mouseX > witchx-125) && (mouseX < witchx + 225 ) && 
        (mouseY > witchy) && (mouseY < witchy + 200) ) {
        click = !click;
      }
      // clicked ();
    }
    
    void clicked () {
      if (click) {
        stageDay(skyX, skyY, skyW, skyH);
      }
      else {
        stageNight(skyX, skyY, skyW, skyH);
      }
    }
    
    //
    
  • edited October 2014 Answer ✓

    your witch doesn't fly at day, but you have to click on her, to make her visible

    submit to halloween contest by @techwiz777 please!

  • Thank you all for your help. Just one more question in the code above what would this mean "Use function arguments to input values needed by functions not globals"?

    Do you not need a globals to set witchx=590; then you can - or + what you need?

  • edited October 2014

    If a variable needs to be accessed across functions &/or needs to persist its stored value, gonna need to be global.

  • Answer ✓

    Instead of having:

    int x = 5;
    
    void f1()
    {
      x *= 2;
      f2();
    }
    
    void f2()
    {
      println(x);
    }
    

    it is better to have:

    void f1()
    {
      int x = 5;
    
      x *= 2;
      f2(x);
    }
    
    void f2(int x)
    {
      println(x);
    }
    

    when it is possible, of course. (And this example is too simplistic...)
    Obviously, in Processing, we still need global variables because setup() cannot pass arguments to draw(), so are keyPressed() and similar...

  • edited October 2014

    clicked() shouldn't be called clicked() but showBackgroundDayOrNight or so...

    when it's intentional that the witch is not shown at day:

    • I suggest at daytime you can click anywhere to make it night

    • because otherwise it's hard at daytime to click on an invisible witch

  • Chrisir add sketch to the link above.

  • thank you!

Sign In or Register to comment.