如何在Java中重置计时器?

时间:2018-11-09 09:46:50

标签: java eclipse timer reset

因此,我试图使用Eclipse Oxygen.2在Java中编写一款名为Lights Out Cube的手持式电子游戏,因此我决定添加一个计时器,以便玩家知道他需要多少时间才能完成游戏。游戏。当玩家单击名为“开始/重置”的按钮(单击一次后将其文本更改为“重置”)时,即启动计时器,游戏开始。每次单击后,我都会检查玩家是否完成了游戏,如果完成了,我将停止计时器。如果他想再次播放,我希望计时器重新启动。请在这里帮助我

//here i have a function called checkIfWinning() which does the checking; if there is a winner, the following code is executed to stop the timer
            timer.cancel();
            timer.purge();
//timer is a publicly declared java.util.Timer
//this is a code snippet for btnStart with "time" recording the time in seconds 
time=0;
timer.schedule(new TimerTask()
            {
                @Override
                    public void run() 
                    {
                            time++;
                            int hours = (int) time / 3600;
                            int remainder = (int) time - hours * 3600;
                            int mins = remainder / 60;
                            remainder = remainder - mins * 60;
                            int secs = remainder;
                            timeTaken.setText(String.format("%02d:%02d:%02d",hours,mins,secs));
                    }

            }, 1000,1000);

无论如何,这可以做到吗?还是我必须完全删除计时器?

2 个答案:

答案 0 :(得分:1)

激活TimerTask对象时无法重置,但在特定情况下,可以重置游戏时间计数而无需删除并重新创建定时器。

由于计时器每秒触发一次,因此,当用户单击重置按钮时,只需重置您正在使用的time变量即可。

我正在根据绿巨人和您的评论来编辑此答案:

  1. 绿巨人是正确的,您应该使用AtomicInteger作为时间计数。如果保持计时器运行,则有时可能会出现一个错误,导致该值不会重置。

  2. 您可以有一个AtomicBoolean标志,使TimerTask知道玩家是否在玩。

这是一个代码示例:

AtomicInteger time = new AtomicInteger();
//whenever you want to reset it:
time.set(0);

AtomicBoolean isPlaying = new AtomicBoolean();

//when user clicks "start":
isPlaying.set(true);
//when user wins or clicks "reset"
isPlaying.set(false);

//your timer task will look something like this:
public void run() {
    if (isPlaying.get()) {
        int currentTime = time.incrementAndGet();
        int hours = (int) currentTime / 3600;
        int remainder = (int) currentTime - hours * 3600;
        int mins = remainder / 60;
        remainder = remainder - mins * 60;
        int secs = remainder;
        timeTaken.setText(String.format("%02d:%02d:%02d",hours,mins,secs));
    }
}

答案 1 :(得分:0)

这可能很清楚。

  1. 您无法在Java中重置或暂停Timer,但可以通过不基于boolean检查运行计时器来实现此功能。

  2. 正如Hulk和Lev.M先前所说,AtomicInteger可用于需要线程安全的情况。

  3. 可以使用run()将秒转换为指定格式来简化TimeUnit中的整个逻辑,

    time+=1000;
    System.out.println(String.format("%02d:%02d:%02d", TimeUnit.SECONDS.toHours(time), TimeUnit.SECONDS.toMinutes(time), time%60));
    

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

public class Game
{
    private static int time = 0;
    private static Timer t = new Timer();
    private static Game g;
    private static boolean ispaused = false;

    public static void main(String[] args)
    {
        t.schedule(new TimerTask() {
            public void run()
            {
                if(ispaused)
                {
                    return;
                }
                time++;
                System.out.println(String.format("%02d:%02d:%02d", TimeUnit.SECONDS.toHours(time), TimeUnit.SECONDS.toMinutes(time), time%60));
            }
        }, 1000, 1000);

        g = new Game();

        try
        {
            System.out.println("Starting first game");

            g.startGame(); Thread.sleep(5000); g.endGame();

            System.out.println("Starting second game.");

            g.startGame(); Thread.sleep(5000); g.endGame();
        }
        catch(Exception e)
        {}
    }

    private void startGame()
    {
        time = 0;
        ispaused = false;
        System.out.println("Game Started");
    }

    private void endGame()
    {
        time = 0;
        ispaused = true;
        System.out.println("Game ended");
    }
};