I want to do the following in Processing:
- Display text on the screen
- When mouse is not pressed, always display the first string in the array
- When mouse is pressed, choose a random string from the array and have this string persist until the mouse is pressed again
I tried moving "int index = int(random(0, 3));" two draw() but this picks a random string many times a second, and I want it to pick it once until I press the mouse again.
In draw(), the value of index is always zero. It only changes in mousePressed().
Here's my code:
String[] sentences = { // defines an array of three strings
"Sentence one",
"Sentence two",
"Sentence three",
};
int index = 0; // initializes variable "index" to zero
void setup() {
size(600, 600);
}
void draw() {
background(255);
fill(0);
text(sentences[index], 10, 100); // picks a random sentence from the array
}
void mousePressed() {
if (mousePressed == true) { // when mouse pressed
int index = int(random(0, 3)); // picks a random value for index: either 0,1,2
}
}