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???
+1
A:
System.currentMiliSeconds(); Save it at the beggining of the game. And then compare it: if(cm<(System.currentMiliSeconds()/1000-60)){System.exit(0);}
oneat
2010-03-11 18:33:35
+2
A:
if you want something interactive you can use TimerTask
and Timer
classes:
class ExpireTask
{
YourClass callbackClass;
ExpireTask(YourClass callbackClass)
{
this.callbackClass = callbackClass;
}
public void run()
{
callbackClass.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(), 60000 /* 60 secs */);
...
Jack
2010-03-11 18:41:18
hi jack many thanks for your reply, I am trying to use this code, but I cant could you please explain more???
2010-03-11 22:05:44
It's quite easy, the only thing you have to provide is a class (in my example **YourClass** that has a method **timeExpired**). Then implement what you want to do when timer reaches 60 seconds inside that method and you are done :)
Jack
2010-03-11 22:20:23