0

I'm creating a game that ends if you haven't reached a certain point by the time the timer reaches zero. It currently works and I can create multiple TimerTasks at one time, but I cannot cancel them. Currently, clicking the button resets the timer (on the display) but will still run in the background and end the program when it reaches zero (even though it shouldn't be running). Here's the code for the ActionListener that starts each timer.

public class Game implements Runnable {

    private int currentScore, difficulty, level, highscore, x, y;
    private boolean playMusic, playClicks, gameRunning;
    private boolean stopLastTimer = false;
    JFrame gameFrame;
    Data data;
    JButton target;
    Thread t;
    JLabel score;
    Timer globalTimer = new Timer();
    JLabel timer;

    ActionListener clickedAction = new ActionListener(){
            public void actionPerformed(ActionEvent ae) {
               stopLastTimer = true;
               t.interrupt(); 
               currentScore++;
               score.setText("Score: " + currentScore);
               //playClickSound();
               globalTimer.schedule(new TimerTask(){

                   double second = 2.0;
                   @Override
                   public void run() {
                       second = second - 0.1;
                        if(stopLastTimer) { 
                        this.cancel(); stopLastTimer = false; } //should end old timer here
                        if(second == 0.0) {
                        this.cancel();
                        gameStop();
                       }
                       second = limitPrecision(Double.toString(second), 1);
                    timer.setText(second + "s");
                   }   
               },0, 100);

            }
        };
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Telle Miller
  • 43
  • 1
  • 1
  • 6

1 Answers1

2

Don't use a TimerTask.

Instead you should be using a javax.swing.Timer for the animation. A Swing Timer has a stop() method.

The source of the ActionEvent will be the Timer itself.

Read the section from the Swing tutorial on How to Use Timer for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • How would I do this? I've read the API and tried the code but it doesn't seem like the type of thing I would use for this situation. I can't even get it to display a countdown. – Telle Miller Mar 25 '16 at 04:11
  • The concept is the same as an AWT Timer, but it is used with Swing so that Swing components are updated properly in the Event Dispatch Thread. Every timer the Timer fires you decrement the second by one. When the second reaches 0 you stop the Timer. The logic is no different then what you current have except the code in in the actionPerformed() method of an ActionListener instead of a run() method. Here is a simple example to get your started: http://stackoverflow.com/questions/30417719/update-a-label-with-a-swing-timer/30417857#30417857 – camickr Mar 25 '16 at 04:49