continous screen switching with 1 click of a button?

Viewed 37

Hello i would like to continously switch between screens with one click of a button using my mouse (Right click) but i dont know how to achieve this? I've managed to switch the screen with the mouse but it stops when it gets to screen 3. I would like to switch all the time everytime i click and not stop on screen 3.

Code:

int screen = 0;
void setup() {
size(200, 100);
}
void draw() {
background(0);
fill(255);
if(screen == 0) {
text("START THE GAME!!!", 20, 50);
}
if(screen == 1) {
text("your on level 1", 20, 50);
}
if(screen == 2) {
text("Ooh man onto level 2", 20, 50);
}
if(screen == 3) {
text("YOU HAVE WON!!! Gratz", 20, 50);
}
}

void mousePressed() {
screen = min(screen + 1, 3);
}
1 Answers

Currently, you set the screen variable to the smaller one of screen + 1 and 3. So when screen reaches 3, and you click your mouse again, it is just set to 3 again.

You can fix this by changing your mousePressed to:

void mousePressed(){
     screen = (screen + 1) % 4;
}

This way, screen increases by one every time you click your mouse, but after it reaches 3 it is set back to 0 on the next click, because 4 % 4 = 0 and you start from screen 0 again.

Related