-2

When I try to make a pause, the screen of the game only freezes and does not make a pause. I mean i want that the game wait and then keep working, not that the window don't respond.

I tried it with Thread.sleep(1000);

if (Var.tropfeny > 760) {     
    Var.tropfeny = -10;      
    Var.tropfenx = Var.n1;   
    Var.playerPoints = 0;    
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Munter3D
  • 1
  • 1
  • 1
    There is not enough information here, but one thing is clear: `Thread.sleep()` is not going to magically do whatever you would expect a "game pause" to do. – domsson Apr 29 '20 at 13:54

1 Answers1

1

By putting Thread.sleep in that place you presumably paused the thread which redraws the window when it changes, that's why the screen freezes when you do it.

The correct way to pause the game is to have a variable which indicates the paused state (something like boolean paused) and just not update the game state at all whenever that boolean is set.

For example, if you had code that said

foo.posX += foo.velocityX; // move the foo

then you change that to

if (!paused) {
  foo.posX += foo.velocityX; // move the foo
}

In an event-based system like a Swing UI you usually don't want to stop processing fully at all. You just want to selectively disable some behaviour.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614