# Randomly Appearing Objects When Something Is Clicked

**URL:** <https://discourse.processing.org/t/randomly-appearing-objects-when-something-is-clicked/21792>\
**Category:** Coding Questions\
**Created:** [June 11, 2020, 8:57pm UTC](https://discourse.processing.org/t/randomly-appearing-objects-when-something-is-clicked/21792 "2020-06-11T20:57:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chemicalite](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@chemicalite](https://discourse.processing.org/u/chemicalite)\
**Post date:** [June 11, 2020, 8:57pm UTC](https://discourse.processing.org/t/randomly-appearing-objects-when-something-is-clicked/21792/1 "2020-06-11T20:57:36Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [June 11, 2020, 9:10pm UTC](https://discourse.processing.org/t/randomly-appearing-objects-when-something-is-clicked/21792/2 "2020-06-11T21:10:41Z")

</div>

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…  
🤓

---

<div class="post-metadata">

**Author:** ![chemicalite](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@chemicalite](https://discourse.processing.org/u/chemicalite)\
**Post date:** [June 11, 2020, 10:46pm UTC](https://discourse.processing.org/t/randomly-appearing-objects-when-something-is-clicked/21792/3 "2020-06-11T22:46:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 11, 2020, 11:18pm UTC](https://discourse.processing.org/t/randomly-appearing-objects-when-something-is-clicked/21792/4 "2020-06-11T23:18:33Z")

</div>

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

> **[reference | p5.js](https://p5js.org/reference/#/p5/string)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

Use conditional logic to check if the corners are clicked …

[![](https://img.youtube.com/vi/r2S7j54I68c/maxresdefault.jpg "3.3: Else and Else if, AND and OR - p5.js Tutorial") ](https://www.youtube.com/watch?v=r2S7j54I68c)

This is probably a better place to start though …  
https://www.youtube.com/embed/videoseries?list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA&wmode=transparent&rel=0&autohide=1&showinfo=1&enablejsapi=1

---

<div class="post-metadata">

**Author:** ![chemicalite](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@chemicalite](https://discourse.processing.org/u/chemicalite)\
**Post date:** [June 11, 2020, 11:39pm UTC](https://discourse.processing.org/t/randomly-appearing-objects-when-something-is-clicked/21792/5 "2020-06-11T23:39:02Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 11, 2020, 11:48pm UTC](https://discourse.processing.org/t/randomly-appearing-objects-when-something-is-clicked/21792/6 "2020-06-11T23:48:18Z")

</div>

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 …

https://www.youtube.com/embed/videoseries?list=PLRqwX-V7Uu6ZYJC7L-r6rX6utt6wwJCyi&wmode=transparent&rel=0&autohide=1&showinfo=1&enablejsapi=1

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 12, 2020, 1:22am UTC](https://discourse.processing.org/t/randomly-appearing-objects-when-something-is-clicked/21792/7 "2020-06-12T01:22:52Z")

</div>

> [@chemicalite](#):
>
> 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.

A typical way to break this down in Processing:

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

…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](https://processing.org/reference/text_.html)

Then, put it all together.

---

<div class="post-metadata">

**Author:** ![chemicalite](https://avatars.discourse-cdn.com/v4/letter/c/82dd89/32.png) [@chemicalite](https://discourse.processing.org/u/chemicalite)\
**Post date:** [June 12, 2020, 1:37am UTC](https://discourse.processing.org/t/randomly-appearing-objects-when-something-is-clicked/21792/8 "2020-06-12T01:37:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 12, 2020, 2:12am UTC](https://discourse.processing.org/t/randomly-appearing-objects-when-something-is-clicked/21792/9 "2020-06-12T02:12:07Z")

</div>

> [@chemicalite](#):
>
> how can I prevent the popup text box from going back so quickly

Think about this:

```auto
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?
