Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Quick Timer Question  (Read 1213 times)
0 Members and 1 Guest are viewing this topic.
Offline Vladiedoo
« Posted 2012-07-13 20:36:25 »

Hi, I'm going to incorporate timers into my Slick 2D game but when I declared
1  
Timer timer = new Timer();

I get three import options.
Do I want to import from java.util, javax.swing, or javax.management.timer?
It works perfectly fine with java.util but I was wondering if there is a significant difference between them.
Thanks for your time and help.
Offline davedes
« Reply #1 - Posted 2012-07-13 20:48:47 »

Don't use any of those -- write your own timer class that utilizes Slick's update loop and delta timing.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
int time = 0;
int delay = 1000; //e.g. 1 second

public void update(int delta) {
    time += delta;
    if (time >= delta) {
       time -= delta
       listener.trigger();
    }
}



Technical note:

You should cap your maximum logic interval (i.e. max delta) using the GameContainer method, otherwise you may get weird timing results when the delta spikes. Alternatively, you can use the following code instead, which works fine for most games although it isn't 100% accurate:
1  
2  
3  
4  
5  
6  
...
if (time >= delta) {
    time = 0;
    listener.trigger();
}
...

Offline Vladiedoo
« Reply #2 - Posted 2012-07-13 22:46:11 »

Doesn't that use CPU whereas a Timer sleeps?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2012-07-13 23:36:49 »

Is the "management" Timer new with Java 7?

I always preferred the util Timer, as the Swing Timer can really clog up the EDT.

You might be interested to know that "Java Concurrency in Practice" (authors include: Biran Goetz, Joshua Bloch, Doug Lea), they say that one should prefer a ScheduledThreadPoolExecutor to a Timer, that it does a better job of handling unchecked exceptions.

http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html
Quote
A ThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically. This class is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ReBirth
« Reply #4 - Posted 2012-07-14 02:46:59 »

Doesn't that use CPU whereas a Timer sleeps?
You want your CPU to idle in the moment?

Offline Vladiedoo
« Reply #5 - Posted 2012-07-14 14:26:21 »

Doesn't that use CPU whereas a Timer sleeps?
You want your CPU to idle in the moment?

Yeah, I only need to do a few if statements every second, nothing in-between.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-07-14 17:10:05 »

Don't use separate threads for logic Wink

Offline Vladiedoo
« Reply #7 - Posted 2012-07-15 03:42:35 »

Don't use separate threads for logic Wink

Sorry for the delayed response and a huge thanks for everyone that replied to this thread, I've learned a lot thus far.
Now that I've thought about it I don't even need a Timer, I can just adjust the code to go into my update method. For future reference though I don't even want to use a Timer or a ThreadPoolExecuter as Philfrei suggest but I'd want to set up what davedes showed me, correct?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (39 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (36 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!