// button variables
int buttonw = 30;
int buttonh = 20;
int buttonx = 20;
int buttony = 50;
boolean buttonON = false;
void setup()
{
size(300, 300);
}
void draw()
{
if (buttonON) {
fill(180);
}
else {
fill(0);
}
rect(buttonx, buttony, buttonw, buttonh);
}
void mouseReleased()
{
if (insideRect(mouseX, mouseY, buttonx, buttony, buttonw, buttonh)) {
// there are multiple ways to write this operation
//1
buttonON = !buttonON;
//2
// buttonON = buttonON == false ? true : false; //
//3
// if (buttonON) {
// buttonON = false;
// }
// else {
// buttonOON = true;
// }
//
//
}
}
// boolean function returns either true or false
// if an x,y point is inside a rectanglular area
boolean insideRect(float px, float py, float rx, float ry, float rw, float rh) {
return px >= rx && px <= rx+rw && py >= ry && py <= ry+rh;
}
Using a BOOLEAN Variable to Store a Button State
Labels:
boolean,
code,
function,
processing