How do I pick a string from an array on mouse press?

Hi, I want to do the following:

  • Display text from an array 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, then choose another string on mouse press and have it persist, and so on

I’ve pasted my code below. I expect it to do what I described, but instead it always displays the first string in the array. The value of “index” changes randomly as expected.

Thanks for your help!

Edit: I also posted this question on Stack Overflow while waiting for my post to be approved: https://stackoverflow.com/questions/72501909/how-to-pick-a-random-string-from-an-array-on-mouse-press-in-processing

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
    println(index); // prints current value of index
  }
}

Hi
remove int index let it index just

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 
    index = int(random(0, 3)); // picks a random value for index: either 0,1,2
    println(index); // prints current value of index
  }
}

1 Like

You can also remove this as it is useless. The mousePressed function is called once if it is pressed …

Cheers
— mnse

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 
    index = int(random(0, 3)); // picks a random value for index: either 0,1,2
    println(index); // prints current value of index
 // }
}

PS: Regarding the last requirement …

  • When mouse is pressed, choose a random string from the array and have this string persist until the mouse is pressed again, then choose another string on mouse press and have it persist, and so on

You should maybe implement, that the randomly selected string is different to the last one…
ie:

void mousePressed() {
   int newindex =  int(random(0, 3)); 
   while (newindex == index) {
       newindex =  int(random(0, 3)); 
   }
   index = newindex;
}

and to be more flexible regarding the amount of sentences …

void mousePressed() {
   int newindex =  int(random(0, sentences.length)); 
   while (newindex == index) {
       newindex =  int(random(0, sentences.length)); 
   }
   index = newindex;
}
3 Likes