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???
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
The thing is that when you schedule a task,that task runs on a different thread, how could you possibly use that to change the player turn which is running on another thread, without making variable checking along the whole execution to know whenever the callback function is called.
I add some sample code as well.
This is my custom Timer class that will call the gameManager method, but this method does not stop the loop of the game to give the turn to another player.
This is the game loop that executes always (I know I should add a condition to stop if the game is paused)
The problem here is what to do a the
changeTurn()
method in order to stop the game loop while it is executing.The first way that comes to my mind is using exceptions, but this wont work because they are two separate threads therefore the exception stops at the CustomTimerTask.
if you want something interactive you can use
TimerTask
andTimer
classes:So now you've got a timer that fires calling
timeExpired
of another class. Now with aTimer
you can schedule it:System.currentMiliSeconds(); Save it at the beggining of the game. And then compare it: if(cm<(System.currentMiliSeconds()/1000-60)){System.exit(0);}