mouseClicked on certain rects
in
Programming Questions
•
1 year ago
How would I go about making mouseClicked only work when clicking on certain rects/objects. I have one square right now that works when the mouse gets clicked, but later on I would like to add more squares and have each one only move when the mouse gets clicked within that square. I'm thinking functions, but not sure how to implement this. If there is a cruder way to accomplish this without functions, that's ok too.
float x, x1, x2, x3, x4, x5, x6 = 40;
float y = 40;
int bxW = 60;
int bxH = 60;
boolean rectClicked = false;
float moveX = 2;
///////////////////////////////
void setup() {
size(700, 600);
smooth();
noStroke();
x = 40;
}
///////////////////////////////////////
void draw() {
background(255);
// draw the ball
fill(#7178E5);
noStroke();
rect(x, y, bxW, bxH);
if (rectClicked) {
x += moveX;
}
if (x>= 400) {
x = 400;
moveX = - moveX;
}
if (x<=40) {
x = 40;
moveX = - moveX;
}
//empty rect
stroke(#3C3D40);
fill(255, 15);
rect(40, 40, 60, 60);
}
///////////objects functions /////////
void mouseClicked() {
rectClicked = true;
}
1