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

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);
}

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

1 Like

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.

1 Like

Hi @emmarope ,

A global variable refers to the notion of scope 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 :

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 :

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 :

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 :slight_smile: )

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

int n = 0;

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

void draw() {
  // Also here
}

Hope it helps!

1 Like