Stoppuhr mit Bedingung funktioniert nur beim ersten Mal?

Ich schreibe ein "Wer wird Millionär" -Spiel und ich habe alles eingerichtet, es ist nur ein Problem mit dem Timer.

Das Spiel funktioniert so: Wenn der Benutzer die richtige Frage hat, geht er weiter. Wenn nicht, ist das Spiel vorbei und sie können sich dafür entscheiden, erneut zu spielen.

Wenn das Spiel zum ersten Mal ausgeführt wird, ist es in Ordnung und der Timer macht das, was er sollte. Beginnen Sie nach 30 Sekunden und zählen Sie nach unten, wobei die Sekunden angezeigt werden.

Wenn der Benutzer jedoch auf die Schaltfläche "Erneut abspielen" klickt, wird der vorherige Timer mit dem neuen Timer fortgesetzt. So was:

-timerA hatte noch 20 Sekunden Zeit, bevor der Benutzer verlor (angezeigt durch a).

-timerB startet das nächste Mal, wenn das Spiel gespielt wird (angezeigt durch b).

Ausgabe: 20a, 29b, 19a, 28b, 18a, 27b, 17a, 26b ... 2a, 11b, 1a, 10b, 9b, 8b, 7b, 6b, 5b, 4b, 3b, 2b, 1b

Also hier ist meine Timer-Klasse namens CountDown:

  import java.util.Timer;
  import java.util.TimerTask;

  public class CountDown {

      static Timer timer;
      public static int seconds = 30;

      public CountDown() {
          timer = new Timer();
          timer.schedule(new DisplayCountdown(), 0, 1000);
      }

      class DisplayCountdown extends TimerTask {

          int seconds = 30;

          public void run() {

                  if (seconds == 0) {
                      timer.cancel();
                      timer.purge();
                      return;
                  }

              if (seconds > 0) {
                  PlayFrame.timer.setText("" + seconds); //jLabel used to display seconds
                  seconds--;
                  if (seconds < 30) {
                      if (PlayFrame.right == true) { //check if question was answered correctly
                          System.out.print("true"); //testing purposes
                          PlayFrame.right = false;
                          PlayFrame.showQuestion();
                          PlayFrame.startTimer();
                          seconds = 0;
                          //break;
                      }
                      else if (PlayFrame.right == false) {
                          //break;
                      }
                  }      

                  else if (seconds == 0) { //if time runs out its automatic wrong answer
                      PlayFrame.wrong();
                      //break;
                  }      

                  else {
                      PlayFrame.wrong();
                      PlayFrame.timer.setText(null);
                      timer = new Timer();
                      //break;
                  }
              }
              System.out.println(seconds); // for testing purposes only
          }
      }
  }

Und hier sind einige meiner PlayFrames:

  import java.awt.Color;
  import java.util.Timer;

  public class PlayFrame extends javax.swing.JFrame {

      public static void wrong() {
          //determines which losing frame to show
          if (count <= 2){
              LossLevel0 L0 = new LossLevel0();
              L0.setVisible(true);
          }
          else if (count > 2 && count <= 6 && count != 6){
              LossLevel1 L1 = new LossLevel1();
              L1.setVisible(true);
          }
          else {
              LossLevel1 L1 = new LossLevel1();
              L1.setVisible(true);
          }
          //"supposed" to stop the timer
          CountDown.timer.cancel();
          CountDown.timer.purge();
      }

      public static void startTimer() {
          //creates new CountDown object and timer, also resets seconds
          CountDown countdown = new CountDown();
          CountDown.timer = new Timer();
          CountDown.seconds = 30;
      }

Ich denke, das Problem kann auftreten, wenn ich das Spiel neu starte. Der einzige Code, der darin enthalten ist, ist, dass ich alle meine Variablen auf den ursprünglichen Zustand zurückgesetzt habe, bevor das Spiel gestartet wurde. Wie so:

    // Reset Everything
    PlayFrame.count = 0;
    PlayFrame.answer = new String();
    PlayFrame.count = 0;
    PlayFrame.right = false;
    PlayFrame.winnings = 0;
    CountDown.seconds = 30;
    CountDown.timer = new Timer();
    CountDown.timer.cancel();
    CountDown.timer.purge();

Bitte helfen Sie, und wenn Sie weitere Informationen benötigen, fragen Sie einfach!

Antworten auf die Frage(3)

Ihre Antwort auf die Frage