游戏的java计时器 [英] java timer for game

查看:125
本文介绍了游戏的java计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用java创建了一个游戏,现在我只需要添加一个允许用户在60秒以下玩的计时器。我在互联网上搜索并找到了swing和util包的计时器。你可以请给我一个方法,以便能够在我的游戏中使用它吗?

I have created a game in java and now I just need to add a timer that allow the user to play under 60s. I have searched on internet and found the timer for swing and util packages. could you please just give me a method to be able to use it in my game???

推荐答案

如果你想要一些互动的东西你可以使用 TimerTask 计时器类:

if you want something interactive you can use TimerTask and Timer classes:

class ExpireTask extends TimerTask
{
  YourClass callbackClass;

  ExpireTask(YourClass callbackClass)
  {
    this.callbackClass = callbackClass;
  }

  public void run()
  {
    callbackClass.timeExpired()
  }
}

所以现在你有一个计时器可以激活另一个类的 timeExpired 。现在有一个计时器你可以安排它:

So now you've got a timer that fires calling timeExpired of another class. Now with a Timer you can schedule it:

...
Timer timer = new Timer();
timer.schedule(new ExpireTask(callbackClass), 60000 /* 60 secs */);
...

这篇关于游戏的java计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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