0

I have a simple game that has collisions and the player will lose life when the player touches an enemy. I have a method that checks if the player isAlive and if the method returns false than the game will stop and will exit the window. If the player is dead I call my stop method which stops the program but keeps the window open and frozen. How could I make it so that the whole window closes and what method would I put it in?

isAlive()

public static void isAlive() {
if(HUD.getHEALTH() < 0) {
    stop();
}

stop()

public synchronized static void stop() {
    try {
        thread.join();
        running = false;
    } catch(Exception e) {
        e.printStackTrace();
    }
}

Sorry if this is a repeat or duplicate I just can't find my specific problem on this website. I've already looked. If you have a possible solution or something that can help please share it. Please and thank you!

Reddish
  • 11
  • 4

2 Answers2

0

Just use System.exit(0) to stop the JVM.

apetranzilla
  • 5,331
  • 27
  • 34
-1

There are multiple ways to shut down the program, but most frequently:

System.exit( 0 );

Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

or if you're using JavaFX...

Platform.exit();

Causes the JavaFX application to terminate. If this method is called after the Application start method is called, then the JavaFX launcher will call the Application stop method and terminate the JavaFX application thread.

Dominic
  • 164
  • 2
  • 11
  • Thank you both. I had to put the System.Exit(0); before my Try Catch statement but thank you. – Reddish Aug 11 '16 at 16:27
  • @Reddish not a problem! If you could select one of our answers as the correct one it would be appreciated. – Dominic Aug 11 '16 at 18:26