We are about to switch to a new forum software. Until then we have removed the registration on this forum.
For class, I need to make a square that cycles through colors when clicked inside the square (black when inactive, then red, then yellow, white and then black again) and then stops on the last color. I've gotten it to change to only red when clicked, and I'm not sure what coding I need to add to get it to cycle through the others and then stop on the first.
Here is my coding so far:
boolean pressInsideButton = false;
boolean isButtonArmed = false;
int x1 = 300;
int y1 = 200;
int x2 = 200;
int y2 = 200;
void setup() {
size( 600, 400 );
}
void draw() {
background( 128 );
if (isButtonArmed == true) {
fill(255, 255, 0);
fill(255, 0, 0);
} else {
fill( 0 );
}
rectMode( CENTER );
rect( x1, y1, x2, y2 );
}
void mousePressed() {
pressInsideButton = false;
if (mouseX <= x1 && mouseX >= x2 && mouseY >= y1 && mouseY >= y2) {
pressInsideButton = true;
}
}
void mouseReleased() {
loop();
if (pressInsideButton && mouseX <= x1 && mouseX >= x2 && mouseY >= y1 && mouseY >= y2) {
isButtonArmed = !isButtonArmed;
}
}
`
Answers
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
fixed the coding
color[]
array in order to store the sequence.color
.We do not offer complete solutions for homework on this forum, but I did rework your sketch to simplify it. I also put in some basic logic for clicking and put comments
At the moment it is incomplete (intentionally) but clicking makes an int have a value of 1. After that click has happened the value of that int will increase. You'll have to associate yellow with 2, white with 3, and black with all values greater than or equal to 4. If you click again then that int will go back to being 1 again (red) and the cycle will repeat
Be sure to look at the output, it will show you the current value of the int: