连续的屏幕切换只需点击一次按钮?

huangapple go评论59阅读模式
英文:

continous screen switching with 1 click of a button?

问题

你好我想通过鼠标的一次点击右键来连续切换屏幕但我不知道如何实现我已经成功地用鼠标切换了屏幕但当它到达第三个屏幕时就停止了我希望每次我点击时都能不停地切换屏幕而不是停在第三个屏幕上

代码

    int screen = 0;
    void setup() {
    size(200, 100);
    }
    void draw() {
    background(0);
    fill(255);
    if(screen == 0) {
    text("开始游戏!!!", 20, 50);
    }
    if(screen == 1) {
    text("你在第一关", 20, 50);
    }
    if(screen == 2) {
    text("哦,进入第二关了", 20, 50);
    }
    if(screen == 3) {
    text("你赢了!!!恭喜", 20, 50);
    }
    }
 
    void mousePressed() {
    screen = min(screen + 1, 3);
    }
英文:

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

得分: 2

目前,你将 screen 变量设置为 screen + 13 中较小的一个。
因此,当 screen 达到 3,并且你再次点击鼠标时,它只会再次被设置为 3。

你可以通过将你的 mousePressed 修改为:

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

这样,每次你点击鼠标时,screen 都会增加一,但在它达到 3 后,下一次点击会将其重置为 0,因为 4 % 4 = 0,然后你会再次从 screen 0 开始。

英文:

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.

huangapple
  • 本文由 发表于 2020年4月3日 20:55:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61012411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定