HELP!! I need help on how to figure out how to use the if, else if and else fucntions correctly

I only started using Processing a few weeks ago and have been asked to set up a square on the screen (Simple).

While the mouse is outside the square it should give output of "Outside". Once I move mouse inside box it should output "Inside".

So far the below code is all I have come up with and it's not working for me. The box will come up but once I add the if and else statements I continue to get errors. I played around with curly brackets but I can't pinpoint the issue.

Can anybody help me please before I give up and throw the laptop into a river!! :((

TIA

float col = 0;
void setup() {
  size(300, 300);
}
void draw() 
{
  background(255);
  fill(0, 255, 0);
  rect(125, 125, 50, 50,5);
}
{
  if ((mouseX>125) && (mouseX<275) && (mouseY<275) && (mouseY>125));

    println("Inside");

  else 
    println("Outside");
} 
Tagged:

Answers

  • edited October 2017
    if( you.check_your_syntax() ){
      you.will_see_that_using_semicolons_in_the_wrong_place = "BAD";
      you.will_press( "Ctrl + t" ); // Auto-format your code.
      you.will_put_brackets_in_the_right_places();
    } else {
      you.will_have_a_time = "BAD";
    }
    

    float col = 0; 
    
    void setup() { // Start of setup() 
      size(300, 300);
    } // End of setup()
    
    void draw() {  // Start of draw()
      background(255); 
      fill(0, 255, 0); 
      rect(125, 125, 50, 50, 5);
    } // End of draw()
    
    // This code is not in a function!
    // Maybe you wanted to do this inside draw()?
    { 
      if ((mouseX>125) && (mouseX<275) && (mouseY<275) && (mouseY>125)); // This semicolon is wrong.
      // It's like saying this:
      /*
      if( whatever ) { // Notice there is no semicolon here usually!
        // Do nothing;
      }
      */
    
      // This code is not associated with the above condition!
      // Maybe you wanted to do this instead of nothing?
      println("Inside");
      else 
      println("Outside");
    }
    
  • edited October 2017

    @TfGuy44 :))

    @supermick83 -- based on the mistakes you are making, in addition to solving your immediate problem I would strongly encourage you to re-read the introductory tutorial materials. Getting a firmer understanding of the basic concepts will save you a lot of time in the long run, as you won't make very simple mistakes over and over:

  • edited November 2017
    float col = 0;
    
    void setup()  {
      size(300, 300);
    }
    
    void draw()  {
      background(255);
      fill(0, 255, 0);
      rect(125, 125, 50, 50,5);
    }
    
      if ((mouseX>125) && (mouseX<275) && (mouseY<275) && (mouseY>125)) {
        println("Inside");
     }
    
      else {
        println("Outside");
    }
    
  • edited November 2017

    O.K.?

  • edited February 2018

    there is only if and else else if just combines the effect by saying if (blah) is not the case, then try if (blahahblah)

    also, you are puttin the if statement outside of draw. in your case, you need it IN draw.

    example:

    void setup() {
      // do sum blah
    }
    
    void draw() {
      // put yer graphics here
      if (blah) {
        foo.bar();
      } else if (sumnotfoobarstuff) {
        bar.foo();
      }  else {
        none.of.the.above();
      }
    }
    

    hope you understand now.

  • He understood back in November 2017. Why re-answer a 4 month old thread?

  • that except he managed to get the brackets wrong... the if/else statement was outside draw... and i think he needs it to be IN draw as there are no other functions...

  • remove closing bracket after rect() and put a new one at the end of the file. should fix it.

Sign In or Register to comment.