gpt4 book ai didi

java - 如何在处理中重新启动游戏?

转载 作者:行者123 更新时间:2023-12-02 09:21:20 25 4
gpt4 key购买 nike

所以我想编写一个处理游戏,你尝试用篮球圈接住下落的球。一切都很顺利,直到某个时候,当你赢或输并按下“重新开始”时,游戏将不会重新开始。这是我的 setup() 方法(仅初始化变量的相关部分):

void setup() {
game = new Game();
scr = new SplashScreen();
gameBackground = new Image();
background = new DynamicBackground();
button = new Rect();

/* Music set */
intro = new Music();
lifeMusic = new Music();
failMusic = new Music();

/* Ball and ring set */
ball = new Image();
ring = new Image();

/* Life points set */
life1 = new Image();
life2 = new Image();
life3 = new Image();
life4 = new Image();
failCount = 3;
sucCount =0;
winStr = "YOU WON!";
gameOverStr = "GAME OVER";
restartStr = "Resetart";

flag = true;
win = new Text();
gameOver = new Text();
restart = new Text();

size(710, 490);

.....
}

这是我的mousePressed方法:

void mousePressed(){
if(mouseX>button.x && mouseX <button.x+button.width && mouseY>button.y && mouseY <button.y+button.height){
loop();
setup();
}
}

如你所见,我尝试做 noLoop()当游戏结束时,当您按下我称为“重新启动”的按钮时,它将 loop()setup() .

为什么我按“重新启动”时游戏卡住了?

最佳答案

setup()由内部回调调用。它无意于调用setup。请注意,在设置中窗口已初始化 (size(710, 490);)。 setup 的调用导致系统挂起。

将静态对象的初始化保留在setup中,但将所有动态对象(“移动”对象)的初始化移至init函数。在 setupmousePressed 中调用此函数:

void init() {
game = new Game();
scr = new SplashScreen();
gameBackground = new Image();
background = new DynamicBackground();
button = new Rect();

/* Ball and ring set */
ball = new Image();
ring = new Image();

// ...
}
void setup() {

size(710, 490);

// init static objects

/* Music set */
intro = new Music();
lifeMusic = new Music();
failMusic = new Music();

// init dynamic objects
init();
}
void mousePressed(){
if(mouseX>button.x && mouseX <button.x+button.width && mouseY>button.y && mouseY <button.y+button.height){
init();
loop();
}
}

关于java - 如何在处理中重新启动游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58672461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com