Java 定时器重用

发布于 2024-09-15 23:42:27 字数 866 浏览 5 评论 0原文

我目前正在尝试制作一个小型的brick-breaker游戏,以有效地使用某种形式的能力提升或奖励。我现在已经大部分实施了。但是我有一个问题。我使用 java.util.Timer 来确定通电持续的时间。最有可能的是,该加电将被多次选择(由随机数生成器)。然而,Java Timer 只能使用一次,在调用它的 cancel() 方法后,就完成了。现在,我将游戏设置为将能量提升标记为已使用,并且不再使用它。有办法解决这个问题吗?以下是选择 LongPaddle 启动时调用的方法:

public void longPaddleTime(int seconds) { //longPaddle Timer Method - gets called when the longPaddle bonus is enabled; shuts off longPaddle after a set amount of time
    timerLP.schedule(new TaskLP(), seconds*1000);

}
class TaskLP extends TimerTask { //The task to be run after the timer in longPaddleTime runs out  
    public void run() {
        longPaddle=false; //Disable LongPaddle
        bonusActive=false;
        LPused=true; //Mark as used
        timerLP.cancel(); //Terminate the timer thread
        timerLP.purge();
    }
}

I'm currently trying to get a small brick-breaker game I made to effectively use some form of power-ups or bonuses. I have have it mostly implemented right now. However I have a problem. I use java.util.Timer to determine how long the power-up lasts. Most likely, that power-up is going to be chosen (by a random number generator) more than once. However, a Java Timer can only be used once, and after it's cancel() method is called, it's done. Right now, I set up the game to mark a power-up as used and to never use it again. Is there a way to get around this? Here's the method that is called when the LongPaddle power-up is chosen:

public void longPaddleTime(int seconds) { //longPaddle Timer Method - gets called when the longPaddle bonus is enabled; shuts off longPaddle after a set amount of time
    timerLP.schedule(new TaskLP(), seconds*1000);

}
class TaskLP extends TimerTask { //The task to be run after the timer in longPaddleTime runs out  
    public void run() {
        longPaddle=false; //Disable LongPaddle
        bonusActive=false;
        LPused=true; //Mark as used
        timerLP.cancel(); //Terminate the timer thread
        timerLP.purge();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑依然 2024-09-22 23:42:27

您不需要取消()您的计时器 - Timer.schedule(TimerTask,长延迟)只会运行指定的任务一次。如果你想终止它正在做的一切,你只需要 cancel() 一个计时器。

对于您的情况(安排一次任务),Timer 类中不需要清理。如果您有一项重复任务并且只想停止该任务,则可以调用 TimerTask.cancel() 来防止它再次发生,同时仍允许将计时器重新用于其他目的。

You don't need to cancel() your timer - Timer.schedule(TimerTask, long delay) will only run the specified task once. You only need to cancel() a timer if you want to terminate everything it's doing.

For your case (scheduling a task once), there's no cleanup required in the Timer class. If you had a repeating task and you wanted to stop just that one task, you could call TimerTask.cancel() to prevent it from reoccuring, while still allowing the Timer to be reused for other purposes.

夜巴黎 2024-09-22 23:42:27

您不必取消 TaskLP 中的计时器。

创建一个位于应用程序范围内的 Timer 对象,并根据需要安排新的 TimerTasks。

顺便说一句,虽然没有正式弃用,但 Timer 功能已被 ScheduledExecutorService。我建议,如果你从头开始使用这个框架。

Executors 实用程序类有一些简化 ScheduledExecutorService 构造的方法。

You don't have to cancel the timer in your TaskLP.

Create a Timer object that lives in Application scope and just schedule new TimerTasks as need arises.

BTW, although not officially deprecated, Timer functionality has been superseeded by ScheduledExecutorService. I suggest, if you start from scratch to use this framework.

Executors utility class has a few methods that simplify the construction of the ScheduledExecutorService.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文