# Text in mousePressed() flashing (using text())

**URL:** <https://discourse.processing.org/t/text-in-mousepressed-flashing-using-text/28479>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [March 12, 2021, 9:01pm UTC](https://discourse.processing.org/t/text-in-mousepressed-flashing-using-text/28479 "2021-03-12T21:01:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![emmarope](https://avatars.discourse-cdn.com/v4/letter/e/ac8455/32.png) [@emmarope](https://discourse.processing.org/u/emmarope)\
**Post date:** [March 12, 2021, 9:01pm UTC](https://discourse.processing.org/t/text-in-mousepressed-flashing-using-text/28479/1 "2021-03-12T21:01:43Z")

</div>

I want the coordinates in the string to change every time the mouse is clicked, but they flash by too quickly to read and I am not sure how to fix, code is below:

PImage cityscape, landscape, sky, moon;

String [] coordinates= {“Utah: 40.693050 N, -111.838330 E”, “Oregon: 45.398290 N, -122.635400 E”, “Kentucky: 39.083730 N, -84.513920 E”, “Dublin: 53.278061 N, -6.149347 E”  
};

float skySlide = 0;  
float cityscapeSlide = 0;

void setup() {  
size(1600, 600);  
cityscape = loadImage(“cityscape.png”);  
sky = loadImage(“sky.png”);  
}

void draw() {  
background(255);

//SKY LOGIC  
if (mouseX\<100) skySlide += 1;  
if (mouseX\>width-100) skySlide -= 1;

//CITYSCAPE LOGIC  
if (mouseX\<100) cityscapeSlide += 3;  
if (mouseX\>width-100) cityscapeSlide -= 3;

//SKY GROUP  
push();  
translate(skySlide, 0);  
image(sky, 0, -993);  
image(sky, 2000, -993);  
image(sky, -2000, -993);  
pop();  
if (skySlide\>2000) skySlide -= 2000;  
if (skySlide\< -2000) skySlide += 2000;

//CITYSCAPE GROUP  
push();  
translate(cityscapeSlide, 0);  
image(cityscape, 0, -348);  
image(cityscape, 2000, -348);  
image(cityscape, -2000, -348);  
pop();  
if (cityscapeSlide\>2000) cityscapeSlide -= 2000;  
if (cityscapeSlide\< -2000) cityscapeSlide += 2000;  
}

void mousePressed(){  
fill(255);  
textSize(30);  
text(coordinates[int(random(coordinates.length))], 600, 300);  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 12, 2021, 10:14pm UTC](https://discourse.processing.org/t/text-in-mousepressed-flashing-using-text/28479/2 "2021-03-12T22:14:55Z")

</div>

The logic is a bit mixed up.

mousePressed() is only called very briefly, hence it doesn’t make sense to use text() there.

Instead store the needed info (coordinates[int(random(coordinates.length))]) in mousePressed() in a global variable and use text() in draw()

Hey, and welcome to the forum!

Great to have you here!

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![emmarope](https://avatars.discourse-cdn.com/v4/letter/e/ac8455/32.png) [@emmarope](https://discourse.processing.org/u/emmarope)\
**Post date:** [March 12, 2021, 10:50pm UTC](https://discourse.processing.org/t/text-in-mousepressed-flashing-using-text/28479/3 "2021-03-12T22:50:43Z")

</div>

Thank you!! Would you mind explaining the global variable and how I use it in the code? I am super new to all of this.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [March 12, 2021, 11:17pm UTC](https://discourse.processing.org/t/text-in-mousepressed-flashing-using-text/28479/4 "2021-03-12T23:17:57Z")

</div>

Hi @emmarope ,

A global variable refers to the notion of [scope](https://en.wikipedia.org/wiki/Scope_(computer_science)) in programming which is the area where a variable name can be used to reference some data.

In most programming languages, variables are said to be local to the current block. A block is a piece of code inside brackets like in a `if` statement :

```auto
if (true) {
  // block
}

```

Local mean that as soon as you exit the block (the closing bracket), all variables declared inside that block are not accessible anymore. Check this example :

```auto
if (true) {
  int n = 0;
}

println(n); // -> n cannot be resolved to a variable

```

So n doesn’t exist outside of the block.

A global variable on the other side is accessible from every part of the program even inside blocks. You just need to declare it outside of every block :

```auto
int n = 0;

if (true) {
  println(n); // -> 0
}

// And also functions
void function() {
  println(n);
}

function(); // -> 0

```

If you run this code in Processing, it’s not going to work because Processing hide a little bit of logic behind the scenes because it wraps your code inside one big class (another cool concept 🙂 )

Anyway in Processing, this is how you declare a global variable :

```auto
int n = 0;

void setup() {
  // I can access n here
}

void draw() {
  // Also here
}

```

Hope it helps!
