URGENT! Need help for test tomorrow!

I have a test tomorrow and I don’t know what is wrong with my code. I am supposed to make one ellipse and a rectangle with a circle following the mouse. The circle is on top of the ellipse but behind the rectangle. How do you make it so that when the mouse is over the rectangle, the rectangle changes color to brown and the background changes to green. I tried the code below but when I place my mouse on the rectangle, only the background changes color, and not the rectangle. Am I doing something wrong? Please help!!!

void setup() {
size(600,600);
background(26,204,243);
}
void draw() {
background(26,204,243);

if ((mouseX >= 200 && mouseX <= 400) && (mouseY >= 100 && mouseY <= 500)) {
  fill(200,200,200);
ellipse(300,100,500,200);

fill(215,23,23);
ellipse(mouseX,mouseY,20,20);

fill(10,35,222);
rect(200,100,200,400);

} else
background(53,148,30);
fill(200,200,200);
ellipse(300,100,500,200);

fill(215,23,23);
ellipse(mouseX,mouseY,20,20);

fill(50,50,50);
rect(200,100,200,400);
}
1 Like

You have {}s around your if block, but not around your else block. Abstractly, your draw() method should look like this:

void draw() {
  if (someCondition) {
    // do something
  }
  else {
    // do something else
  }
}

if and else will work without brackets after, but if you don’t put brackets, they’ll only process the very first line following the if or else. Since your background() is the first thing declared after the else, that’s why only the background is changing.

1 Like