英文:
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 + 1
和 3
中较小的一个。
因此,当 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论