0

I searched around for this and couldn't get a clear answer. I've written a game that needs to pause until the user clicks a button with their decision, and then continue to execute. Is there a standard way to do this?

I've seen similar questions that refer to using 'wait()' and 'notify()', but I wasn't sure I needed to add more threads, especially since I'm not executing complex or time-consuming code.

I should clarify it's a computer version of a board game, so nothing more than a frame with some components. Here's some of what I'm trying to do, thanks guys:

public class TreasureHunterFrame extends javax.swing.JFrame
{

    public TreasureHunterFrame()
    {
        initComponents();

        startNewGame();
    }

    private void startNewGame()
    {
        ...
        // User asked to click button while this method is running
        synchronized (this) // wait until Stay of Leave button is clicked
    {
        try 
        {
            while (!userHasMadeDecision)
        this.wait();
        }
        catch (InterruptedException ie)
        {
        }
        }
        ....

    }

    private void userStayButtonActionPerformed(java.awt.event.ActionEvent evt)
    {                                
        userHasMadeDecision = true;
        userLeaving = false;
        synchronized (this)
        {
        notifyAll();
        }
    }          

    public static void main(String args[])
    {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TreasureHunterFrame().setVisible(true);
            }
        });
    }
}
fergusrm
  • 23
  • 5
  • 1
    Try posting one or two things you actually tried that didn't do what you wanted them to. – G. Bach Mar 11 '13 at 04:05
  • 1
    This will depend on how your game engine works. If you have your on thread, you can establish a wait state. We need more info before we can find a solution for you – MadProgrammer Mar 11 '13 at 04:10
  • See [this](http://stackoverflow.com/questions/13999506/threads-with-key-bindings/14001011#14001011) answer its [variation](http://www.daniweb.com/software-development/java/code/444547/game-development-loop-logic-and-collision-detection-java-swing-2d) – David Kroukamp Mar 11 '13 at 14:26
  • For better help sooner, post an [SSCCE](http://sscce.org/). It probably needs a modal `JDialog`. – Andrew Thompson Mar 14 '13 at 03:14

0 Answers0