How can you implement some kind of timer, to know how much time did user spend for one round of a game?
Getting System.currentTimeMillis will get your system time to the millisecond. If you want to do a Swing event intermittently, such as displaying the current time in a stop watch or a simple animation, then a Swing Timer would work well.
One can easily find the time spent by a user on one round of a game by using System.nanoTime() function. However if you want to implement in a game, you would probably like the timer to also be displayed to the user, you can you use multi-threading for the same.
Related
I am writing to see what everyone's way of doing this is.
Example:
I have multiple timers with a start time of potentially anything. E.G, One timer could be 30 seconds, the second could be 10 minutes, the third could be 3mins 32seconds, and so on... You can create infinite timer countdowns.
Now, the question is... What's the best way to detect when a timer hits 0? Java does have a runnable class and I was going to run an async task to check every second but this sounds really inefficient to me.
Are there any other ways to do it? Thanks!
There is a game where 10 minutes of real life equals 60 minutes on his day. So one day in the game equals 4 hours in our real time.
The user will inform me of the time of his game on his first time in the application, and I will need to show the game time to the user every time he enters the application. I tried doing this using a SensorService and a BroadcastReceiver, so that the calculation was always being done.
But since there is no way to get this time the same way we get the normal time, I had to do Background calculations to keep the time counting. This brought me trouble because the time was NEVER displayed accurately, not to mention the problem that is when the user has their cell phone turned off and everything.
I would really like to know if there is a simpler way to do this on Android, I'm new to this area and I do not know if there is a custom speed-time format or something. Thank you all at once.
save the first launch time(normal time).
on succeeding logins game time=(System.currentTimeinMilis() -first launch) * 6
I am making a text-based RPG for a personal project in Java. In the game the protagonist will have at his/her disposal the ability to use a spell or ability. I would like to add a cool down to the aforementioned spell or ability. I have researched both the javax.swing.Timer and java.util.Timer classes but have not found a way to go about using this cool down. I am not using threads or daemons (primarily because I don't think I need to and I have zero experience with them).
Any help or ideas on how to go about this would be appreciated, thanks.
Alumnus
Just save System.currentTimeMillis() at the time the spell is cast and check the time elapsed when the hero tries to cast it again.
When you use your spell, set some variable to say that the spell is now unavailable. Then start a timer that will act as your cool down. When the timer ticks, reset that variable to indicate that it has cooled down and can be used again.
If possible you could save the current game time when the spell/ability gets triggered inside your application and disable the ability.
Then again if yo can do it you can check every "frame update" if the cool down time has already elapsed by computing the time difference between the current time and the time the spell/ability got triggered and reenable the ability when the desired time has elapsed.
If this RPG is turn based combat then do this:
Make a cooldown variable with the number of turns needed to wait for cooldown.
Now make another variable for current cooldown turn, now set its value to the same as the cooldown like
curcooldown = waitforcooldown, something like that.
And then if you check that the player wants to use the spell and then check it like this.
If player wants to use spell & curcooldown is 0
UseSpell
Else if (player wants to use spell & curcooldown greater or equal to 0
saytoplayerthatspellisundercooldown
and if spell is used then curcooldown = waitforcooldown
set it to the cooldown value turn value.
and every time a turn ends then decrease the curcooldownvalue by 1 that means your cooldown just lost 1 turn and so on...
Hope this helped
I'm writing a Java Application that will side scroll sheet music across the screen and then when it crosses a middle line, it will play the note(s).
It will have a variable speed based on the beats per minute.
Every quarter of a beat I need to have a "tick" function that will get the next note (if any) and start its animation. To calculate this tick I have a function 60000ms/bpm/4. This will be separate from the animation timer, because the repaint should probably be called at some constant rate.
So I'm thinking I need Util Timer for the "ticks" and a swing timer for the JPanel and drawing in the paintComponent() method.
My question is: What would be the best practice for accomplishing this? I've done a fair amount of Java programming, but no Swing or animations so I would like to learn the best way to do this.
Thanks
Nate
There is no reason to use Timer or TimerTask to solve this problem.
In this case, you have a series of notes, and when you start playing, you can calculate ahead of time, the exact time that each and every note should play. I am assuming that you already have some sort of loop that is updating the display at a steady rate. (e.g. 30 refreshes per second, or something like that). Given such a loop, all you need is an event oriented class that can tell you whether it is time to play a note.
long noteTime[] = new long[numberOfNotes];
long startTime = System.currentTimeMillis();
Declare the above array. Then walk through all the notes in the song, and calculate the time that each note is expected to play. Say you have qurter notes, the first note will play at startTime, the second one beat after startTime, the third two beats after start time, etc. This is where you calculate the time for a beat and use it. Just calculate the actual time values for each note to play.
Then, in the middle of your event loop that is refreshing the display, include the following code:
int nextNote = 0;
while (event_loop_condition) {
.
.
.
if (System.currentTimeMillis()>noteTime[nextNote]) {
playNote(nextNote++);
}
.
.
.
}
The point is that you already have an event loop, all you need to know is whether it it time yet to play the note, and this will do that for you.
On the other hand, if you are not handling the refresh, and you really do want to do this on a thread, the follow method can be called on a thread, and it will play all the notes at the correct times:
playAllNotes() {
for (int i=0; i<numberOfNotes; i++) {
Thread.sleep(noteTime[i]-System.currentTimeMillis());
playNote(i);
}
}
The sleep statement will delay until the time that the note should be played, and will play it at that time. Then it will sleep until the time for the next note to be played.
The important thing to notice about both methods, is that any delay in playing a note is NOT compounded for the next note. Say some system utility kicks in and delays one note by 50ms. The next note will not be delayed by this, because you calculated all the times for all the notes to be played up front. Given that threads are competing for CPU time, you will have thread/process contention, and there will be small variances in the time that notes are played, but they will never compound!
A lot of programmers inappropriately use timers (See discussion of Timers) on my website if you want to know more.
I have a 4x4 widget and i want to update a little piece of it every 15-20 seconds. Clearly i don't want it to be updated when the phone is in standby. The widget needs also to respond to some system events other than my timer. So, which is the best option?
An AlarmManager: nice but probably cpu intensive if it needs to be run every 20 seconds
An Handler: light but i dont know how to stop it when phone sleeps
A service: also here i need to understand how to stop it when phone sleeps
I will also need to update a little part of my widget without updating all its screen area, is this possible??
Thanks everybody.
I have a 4x4 widget and i want to
update a little piece of it every
15-20 seconds.
Please make this optional or configurable.
So, which is the best option?
A Handler has nothing to do with app widgets.
An always-running service is bad for business and is the sort of thing that causes users to attack you with task killers.
The best of the bad options is AlarmManager.
Frankly, the best answer is "don't update a little piece of it every 15-20 seconds".