2 Replies Latest reply: Jan 19, 2010 5:46 PM by 843853 RSS

    Waiting for a frame to finish

    843853
      So here's what's currently happening-
      I have a newGame() method that does a number of things, mainly reset the game.
      One of these activities involves calling the setDifficulty() method of another class. One difficulty option is "custom," which opens a new frame with the various options displayed, plus a done button.

      I originally had issues getting the game to stop and wait for the button to be pressed, but fixed that with a CountDownLatch. However, I am still having problems.

      What happens is this- if the await() method is called outside of an invokeLater block/method, then the applet stops everything (including loading the custom frame, meaning I have an empty frame and a frozen applet). If the await() method is called within an invokeLater block/method, then it only forces the invokeLater's run() method to wait, so the game thread continues, a new game is started, etc.

      How do I fix this so that everything waits for the input properly? Here is my newGame() method:
      public void newGame()
          {
              //reset score, player, keysPressed list, and icicles
              player.setVisible(true);
              score = 0;
              keysPressed.clear();
              waitingIcicles.clear();
              movingIcicles.clear();
      
              //Get/set difficulty, with CountDownLatch so it waits for done button to be pressed
              doneSignal = new CountDownLatch(1);
              gameData.setDifficulty(diffGroup.getSelection().getActionCommand(), doneSignal);
              SwingUtilities.invokeLater(new Runnable() {public void run() {
                  System.out.println("invoked later"); //Testing to ensure it is running correctly
                  try {doneSignal.await(); }
              catch(InterruptedException e){System.out.println("Caught an Exception. \nExcept you do no care much. \nBut you must fix it.");}
              }});
              
      
              //Init icicle list with a random number of icicles
              int numStartIcicles = r.nextInt(ALLOWANCE)+ MIN_ICICLES;
              for(int i=0; i < numStartIcicles; i++)
                  waitingIcicles.add(new Icicle());
      
              //start playing again
              player.reset();
              repaint();
              start();
          }
      Note that the setDifficulty method counts down the CountDownLatch on its own.