Skip to content

Instantly share code, notes, and snippets.

@sabotai
Last active March 2, 2016 23:21
Show Gist options
  • Save sabotai/c25e32c61cc7666175ec to your computer and use it in GitHub Desktop.
Save sabotai/c25e32c61cc7666175ec to your computer and use it in GitHub Desktop.
Tricky button example from GD105 Week 5
int rectX, rectY, rectW, rectH;
void setup(){
size(1024, 640);
rectX = 200;
rectY = 200;
rectW = 100;
rectH = 75;
}
void draw(){
background(0);
if ((mouseX >= rectX) && //the cursor must be to the right of rectX AND
(mouseX <= rectX+rectW) && //the cursor must be to the left of the X and the rects width AND
(mouseY>rectY) && //the cursor must be below the top edge AND
(mouseY<rectY+rectH)){ //must be above the bottom edge (rectY+rectH)
fill(random(255)); //if those 4 conditions are true, change the fill to a random BW value
if (mousePressed){ //if all those conditions above are true AND the mouse is pressed, do the following...
//background(random(255)); //maybe change the background randomly
rectX = int(random(width - rectW)); //determine a new random x position
rectY = int(random(height - rectH)); //determine a new random y position
}
}
rect(rectX,rectY,rectW,rectH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment