如何在Java中重置计时器?
因此,我正试图使用Eclipse Oxygen.2在Java中编写一个名为Lights Out Cube的手持电子游戏,我决定加入一个计时器,让玩家知道完成游戏需要多少时间。当玩家点击一个名为“开始/重置”的按钮时(点击一次后,我将其文本更改为“重置”),在这里我激活计时器,游戏开始。每次点击后,我都会检查玩家是否完成游戏,如果他完成了,我会停止计时。如果他想再次比赛,我希望计时器重新启动。请帮帮我:如何在Java中重置计时器?,java,eclipse,timer,reset,Java,Eclipse,Timer,Reset,因此,我正试图使用Eclipse Oxygen.2在Java中编写一个名为Lights Out Cube的手持电子游戏,我决定加入一个计时器,让玩家知道完成游戏需要多少时间。当玩家点击一个名为“开始/重置”的按钮时(点击一次后,我将其文本更改为“重置”),在这里我激活计时器,游戏开始。每次点击后,我都会检查玩家是否完成游戏,如果他完成了,我会停止计时。如果他想再次比赛,我希望计时器重新启动。请帮帮我: //here i have a function called checkIfWinning(
//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);
有没有办法做到这一点?或者我必须完全删除计时器吗?当TimerTask对象将被激活时,您无法重置,但在您的特定情况下,您可以重置游戏时间计数,而无需删除并重新创建计时器 由于计时器每秒启动一次,因此当用户单击“重置”按钮时,您只需重置正在使用的
time
变量
我根据Hulks和您的评论编辑此答案:
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));
}
}
这可能很清楚
布尔检查运行计时器来实现该功能
AtomicInteger
可以用于线程安全的需要run()
中的整个逻辑可以通过使用TimeUnit
将秒转换为指定格式来简化
time+=1000;
System.out.println(String.format("%02d:%02d:%02d", TimeUnit.SECONDS.toHours(time), TimeUnit.SECONDS.toMinutes(time), time%60));
请注意,
time
变量是从另一个线程(计时器)访问的,因此从外部设置它需要某种同步。最简单的方法可能是将其转换为AtomicInteger
是的,我考虑过了,但如何冻结屏幕上的时间?顺便说一下,我每次都会将计数重置为0。我试过了,效果很好。但问题是,每当计时器运行得更快时,即每秒更新计时器2秒,然后是3秒和4秒,依此类推。听起来你仍然有一行代码,在玩家每次开始游戏时都会创建新的计时器。如果不删除或取消计时器,则绝对不能创建新的计时器。我重新检查了代码,计时器与公共变量一起公开声明,这只是btnStart代码中的任务。您可以取消当前的TimerTask
,然后提交一个新的。为了能够取消它,您需要在某处保留对该任务的引用。另请参阅使用a的替代方法,它与计时器类似,但更灵活。也许我遗漏了一些非常明显的东西,但为什么您需要这个计时器?你不能记录用户开始和结束的时间,然后从中减去一个吗?
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");
}
};