有条件的停止计时器只能第一次使用? [英] Stop timer with conditional only works first time?

查看:125
本文介绍了有条件的停止计时器只能第一次使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个谁想成为一个百万人的游戏,我已经设置了一切,这只是计时器的一个问题。

I'm writing a "Who Wants to Be a Millionare" game and I have everything set up, it's just a problem with the timer.

游戏有效像这样:如果用户得到正确的问题他/她继续前进。如果没有,游戏结束,他们可以选择再次玩。

The game works like this: if the user gets the question right he/she moves on. If not, the game is over and they can opt to play again.

当游戏第一次运行时,它很好并且计时器完成应有的操作 - 从30秒开始并倒计时,显示秒数。

When the game first runs it's fine and the timer does what it should -- start at 30 seconds and count down, displaying the seconds.

然而,当用户点击再次播放按钮时,前一个计时器继续使用新计时器。像这样:

However, when the user clicks the "play again" button, the previous timer continues with the new timer. Like this:

-timerA在用户输掉前20秒(由a表示)。

-timerA had 20 seconds left before the user lost (indicated by a).

- timerB在下次比赛开始时开始(由b表示)。

-timerB begins the next time the game is played (indicated by b).

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

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

所以这是我的计时器类CountDown:

So here is my timer class called 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
          }
      }
  }

这是我的一些PlayFrame:

and here is some of my PlayFrame:

  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;
      }

我认为当我重新开始游戏时可能会遇到麻烦。唯一的代码是我在游戏开始之前将所有变量重置回原始状态。像这样:

I think the trouble may be in when I restart the game. The only code that is in there is I reset all of my variables back to its original state before the game started. Like 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();

请帮助,如果您需要更多信息,请询问!

Please help, and if you need further information just ask!

推荐答案

解决了,谢谢链接sage88! http://albertattard.blogspot.com/2008/09 /practical-example-of-swing-timer.html

Solved, thanks for the link sage88! http://albertattard.blogspot.com/2008/09/practical-example-of-swing-timer.html

有关摆动计时器的更多帮助(对于此主题的未来搜索) http://www.asjava.com/swing/java-timer-tutorial/

And for more help on swing timers (for future searches of this topic) http://www.asjava.com/swing/java-timer-tutorial/

public static void startTimer() {
    listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            System.out.print("action");
            timerLabel.setText("" + seconds);
            seconds--;
            System.out.println(seconds);
            if (seconds < 0){
                System.out.print("zero");
                wrong();
            }
        }
    };
    displayTimer = new Timer(1000, listener);
    displayTimer.setInitialDelay(1);
    displayTimer.start();

    if (right == true){
        System.out.print("true");
        displayTimer.stop();
        right = false;
        seconds = 30;
        displayTimer = new Timer(10000, listener);
        displayTimer.setDelay(10000);
        displayTimer.setInitialDelay(1);
        displayTimer.start();
    }
    else if (right == null){
        System.out.print("null");
        displayTimer.stop();
        seconds = 30;
        displayTimer = new Timer(10000, listener);
        displayTimer.setInitialDelay(1);
        displayTimer.setDelay(10000);
        displayTimer.start();
    }
}

这篇关于有条件的停止计时器只能第一次使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆