So when a player dies in my game, I want a popup to ask if they want to restart or exit. The NO and CANCEL options work fine (System.exit(0)) but the YES option is where I am having troubles. I call the main game when YES
private void playerHealth() {
JDialog.setDefaultLookAndFeelDecorated(true);
if (player.health <= 0) {
int response = JOptionPane.showConfirmDialog(null, "Do you want to continue?", "Confirm",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
System.exit(0);
}
else if (response == JOptionPane.YES_OPTION) {
new Game().start();
}
else if (response == JOptionPane.CLOSED_OPTION) {
System.exit(0);
}
}
}
using a line from my Launcher Class
package core;
public class Launcher {
public static void main(String[] args){
//Launches new Game
new Game().start();
}
}
and it restarts, but the popup doesnt go away. The game cannot be played again, I have to exit and start it again. It would be good if someone can figure out a way stop the prompt to popup again and again after clicking YES.
Thank you
Edit: I put
player.health = 100;
in the code:
else if (response == JOptionPane.YES_OPTION) {
player.health = 100;
new Game().start();
}
and this seems to fix the problem. However, I now have a completely different problem in that a new app is started every time and the old is not deleted. Is there a way to delete the old and start a new game?
Thanks
Look at
health
variable.If you told the popup shows again, this condition:
has to be true after restart. Because of this I expect
player
orhealth
variable is static and static variables has to be restarted too