Randomly Appearing Objects When Something Is Clicked

Hey everyone!

I’m currently working on a coding project and needed a bit of help on how to do something. I want to be able to have a different “text box” appear every time the user clicks the corner of the canvas. The text box and everything will essentially be the same and will be in the same spot every time the user clicks the corner of the canvas, though the text inside will simply be different every time. (It’s okay if the text repeats sometimes.)

Can someone please help me out with this? Any feedback is greatly appreciated! Thank you :slight_smile:

1 Like

Hello @chemicalite,
Please post your code attempt so far so we can better answer your question. Otherwise, we can only guess what you do and don’t understand…
:nerd_face:

Ah, I don’t have an attempt… I have no idea how to do this :sweat_smile:

You can create a variable that stores a string and update that variable with new text every time the user clicks a corner.

Use conditional logic to check if the corners are clicked …

This is probably a better place to start though …

That’s awesome! I’m super new to processing, and had no idea where to start- those resources and your suggestions are extremely helpful though! Thank you so much!

Sorry, I failed to see this was a processing question, those links are for p5.js. Same principles though, here is a good place to start for processing …

1 Like

A typical way to break this down in Processing:

  1. use “mouseReleased()” for your click code mouseReleased() / Reference / Processing.org
  2. store some text in an array or StringList Array / Reference / Processing.org StringList / Reference / Processing.org
  3. you need to pick a random number from 0-N to choose one of your array or list items. random() / Reference / Processing.org

…and to do that, you need to make your random number an integer. That last reference page actually has an example of getting a random string element!

  1. Once you got your random String, you need to display it – for example, with text() text() / Reference / Processing.org

Then, put it all together.

1 Like

Thank you so much, everyone! I was able to figure it out and I’m quite pleased with the result! I had an additional question- how can I prevent the popup text box from going back so quickly, or how can I delay the code in some way to make the text box stay on the screen for two to three seconds?

Think about this:

if(something) {
  text( txt, 100, 100);
}

How would you clearly explain what the “something” test should be for whether draw should show the text or not? Try thinking it through in English, but as if you were explaining it to a robot who is wondering – how do I know whether I should show text?