button help
in
Programming Questions
•
1 year ago
I am having trouble with buttons. I am trying to make it so red button stops the ball and green button continues moving the ball.
when i have only the first if statement, i can click anywhere on the screen and the ball will stop, i am trying to make it so only clicking the button makes it stop.
also when i add the second if statement, neither mousePressed action works.
I sat down with my professor for an hour after class and he could not spot my error, so he asked me to send it to him.
Maybe someone here can help me out!
Thank you in advance, here is my code.
// set int ellipse
int bX = 50;
int bY = 110;
int bW = 50;
int bH = 50;
//set int large rectangle
int r1X = 10;
int r1Y = 10;
int r1W = 380;
int r1H = 200;
//set int red button
int r2X = 25;
int r2Y = 225;
int r2W = 50;
int r2H = 25;
//set int green button
int r3X = 125;
int r3Y = 225;
int r3W = 50;
int r3H = 25;
//set int blue button
int r4X = 225;
int r4Y = 225;
int r4W = 50;
int r4H = 25;
//set int yellow button
int r5X = 325;
int r5Y = 225;
int r5W = 50;
int r5H = 25;
//set int speed
int speed = 1;
boolean isMoving = true;
void setup() {
size(400,400);
}
void draw() {
background (255);
//set modes to CENTER
rectMode (CORNER);
ellipseMode (CENTER);
//draw large rectangle
fill(255);
rect(r1X,r1Y,r1W,r1H);
//draw red button
fill(255,0,0);
rect(r2X,r2Y,r2W,r2H);
//draw green button
fill(0,200,0);
rect(r3X,r3Y,r3W,r3H);
//draw blue button
fill(0,0,200);
rect(r4X,r4Y,r4W,r4H);
//draw yellow button
fill(255,255,0);
rect(r5X,r5Y,r5W,r5H);
// draw ellipse
fill(175);
ellipse(bX,bY,bW,bH);
// set speed to ellipse
if (isMoving) bX = bX + speed;
//make ellipse bounce off right wall
if ((bX > width-35) || (bX < 35)) {
speed = speed * -1;
}
}
//red button makes ball stop
void mousePressed() {
if (mouseX > r2X && mouseX < r2X+r2W && mouseY > r2Y && mouseY < r2Y+r2H); {
isMoving = false;
}
if (mouseX > r3X && mouseX < r3X+r3W && mouseY > r3Y && mouseY < r3Y+r3H); {
isMoving = true;
}
}
1