简体   繁体   English

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

[英]How can I restart a game in Processing?

So I wanted to write a processing game where you try to catch a falling ball with basketball ring.所以我想写一个处理游戏,你试着用篮球圈接住一个落下的球。 It all goes pretty well until at some point, when you win or lose and press "restart" the game won't restart.一切都很顺利,直到某个时候,当您输赢并按下“重新开始”时,游戏将不会重新开始。 This is my setup() method (only the relevant part of initializing variables):这是我的 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);

    .....
}

and this is my mousePressed method:这是我的mousePressed方法:

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

as you see, I tried to do noLoop() when the game is over, and than when you press on the button I called "restart" it will loop() and setup() .如您所见,我尝试在游戏结束时执行noLoop() ,而不是当您按下我称为“重新启动”的按钮时,它将loop()setup()

The game get stuck when I press "restart" why?一按“重启”,游戏就卡住了,为什么?

setup() is called by an internal callback. setup()由内部回调调用。 It is not intended to call setup .它不打算调用setup Note in setup the window is initialized ( size(710, 490); ).注意在设置中 window 已初始化( size(710, 490); )。 The call of setup causes the system to hang. setup 的调用导致系统挂起。

Keep the initialization of the static objects in setup , but move the initialization of all the dynamic objects (the "moving" ones) to an init function.将 static 对象的初始化保留在setup中,但将所有动态对象(“移动”对象)的初始化移动到init function。 Call this function in setup and mousePressed :setup和 mousePressed 中调用此mousePressed

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM