I think this is a question about program flow and how to enable user interaction.
Currently working on a problem in Shiffman book.
- I want to create multiple buttons (currently 2 buttons in this version) via object class iteration.
- When I mouse click within a button area, button color changes.
I am not seeing the desired on / off interaction via boolean switch.
Code below:
////////////////////////////////////////////////
Button b1;
Button b2;
void setup() {
size (600, 600);
b1 = new Button(random(width), random(height), random(50, 100));
b2 = new Button(random(width), random(height), random(50, 100));
}
void draw() {
b1.display();
b2.display();
b1.click();
b2.click();
}
void mousePressed() {
if (mouseX > x && mouseX < x + sz && mouseY > y && mouseY < y + sz) {
on = !on;
}
}
CLASS//////////////////////////////////////////////////
class Button {
float x;
float y;
float sz;
boolean on;
Button(float tempX, float tempY, float tempSz) {
x = tempX;
y = tempY;
sz = tempSz;
on = false;
}
void display() {
rect (x, y, sz, sz);
}
void click() {
if (on) {
fill (255);
stroke(0);
} else {
fill (0);
stroke (255);
}
if (mouseX > x && mouseX < x + sz && mouseY > y && mouseY < y + sz) {
on = !on;
}
}
}
Any guidance most welcomed!!