How to implement a relative timer in a game?

Java gui countdown timer and increment question?

  • Hi =] I'm fairly new to java and I've recently been trying to make a Mafia war type game with java gui's and so far it is going great except for this one problem. The player has energy(int) which is decreased whenever the player performs an action that requires energy, what I would like to do is say for example: The player has 10 energy and performs an action that uses 1 energy, A JLable displays how much energy the users has at the top i.e: 9/10, As soon as the players energy is below 10 I would like a countdown timer to begin which once it reaches zero,the players energy increases by one, If the players energy still has less than 10 energy, the countdown timer repeats until the players energy is maxed. or... How would I implement a class/method that runs a countdown when the players energy is below max energy, and increments the players energy by one when the countdown reaches zero, repeats previous step until player energy == max energy. Any help would be great =D Sorry for the long post, Thank you for reading =]

  • Answer:

    Have an if statement that checks for when to start the timer. So either if(energy == 0) { //Start timer } or if(energy < maxEnergy) { //Start timer } As for how to use a timer I'd suggest reading the doc I included in my sources. 1 - Basically you'd tell the timer to start from one of those if statements. 2 - Set the timer to go every second and have it update a global int variable (i.e. count) 3 - whenever count gets to a certain number (say 60, so every minute) set count to 0, add 1 to energy and check if energy is equal to maxEnergy 4 - If it is, stop the timer 5 - If it is not, don't do anything (the timer will continue going) Note: The timer should be a global variable and shouldn't be declared within the if statement when you want to start it

Eun at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Have an if statement that checks for when to start the timer. So either if(energy == 0) { //Start timer } or if(energy < maxEnergy) { //Start timer } As for how to use a timer I'd suggest reading the doc I included in my sources. 1 - Basically you'd tell the timer to start from one of those if statements. 2 - Set the timer to go every second and have it update a global int variable (i.e. count) 3 - whenever count gets to a certain number (say 60, so every minute) set count to 0, add 1 to energy and check if energy is equal to maxEnergy 4 - If it is, stop the timer 5 - If it is not, don't do anything (the timer will continue going) Note: The timer should be a global variable and shouldn't be declared within the if statement when you want to start it

Scott

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.