2

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Rob Bor
  • 23
  • 5
  • From the code you have put here, the popup should ideally disappear. Probably the problem is in another place. Try to provide a code using which we can easily reproduce the problem. Having said that, You should reconsider this design, this just keeps creating new game objects for every "Yes option" from within another game object. – Codebender Jun 10 '15 at 10:17
  • I'll try to recreate it somewhere, but you bring up an interesting point about creating another game object every time I click YES. Is there anyway to delete the old and start a new one? – Rob Bor Jun 10 '15 at 10:25
  • You can start the `new Game().start();` in another thread. That way, the current thread will continue executing and finish (thereby getting Garbage Collected). Or you can actually have a restart mechanism where you use the same object but just by resetting all the variables and values. – Codebender Jun 10 '15 at 10:28
  • Another way to notify the 'old' game to stop is to use an [Observer pattern](http://www.oodesign.com/observer-pattern.html). For instance, the playerHealth would notify the game that it needs to close when the player is dead. – Laurent B Jun 10 '15 at 10:38
  • @LaurentB Interesting. How would this work in my code? Sorry, I am not well versed in Java AT ALL – Rob Bor Jun 10 '15 at 10:41
  • 1
    Too long to explain in comment have a look at ActionListener in java and how it is used. It is a good topic to start good coding practices – Laurent B Jun 10 '15 at 10:45

2 Answers2

2

Look at health variable.
If you told the popup shows again, this condition:

player.health <= 0

has to be true after restart. Because of this I expect player or health variable is static and static variables has to be restarted too

maskacovnik
  • 3,080
  • 5
  • 20
  • 26
1

I think the problem is not in the code of your playerHealth method. This should work perfectly fine.

The problem is that probably elsewhere in your code you keep calling back playerHealth.

Maybe you should consider passing the game instance to your player health and call a game.restart() that should stop calling the playerHealth method and restart altogether your game (maybe through game.start()).

To conclude, your code is good but most probably your are doing the call to playerHealth wrong somewhere.

Edit : if you want to use a pattern such as Observer to notify the Game instance it needs to stop, have a look at this post: Examples of GoF Design Patterns in Java's core libraries

Especially the ActionListener instances.

Community
  • 1
  • 1
Laurent B
  • 2,200
  • 19
  • 29
  • Nice idea +1, otherwise I think it is possible only with another thread lost in old object, or lost reference somewhere in the code, it is surely possible – maskacovnik Jun 10 '15 at 10:26
  • No Im not calling playerHealth anywhere but the last line which is an update of the game. If I take it out, player.health will continue to go past 0 and not stop. – Rob Bor Jun 10 '15 at 10:38
  • So you mean the method is in the Game class ? – Laurent B Jun 10 '15 at 10:41