Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (603)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (650)
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 1538 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
Timer timer = new Timer();

I get three import options.
Do I want to import from java.util, javax.swing, or
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.

int time = 0;
int delay = 1000; //e.g. 1 second

public void update(int delta) {
    time += delta;
    if (time >= delta) {
       time -= delta

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:
if (time >= delta) {
    time = 0;

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.
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.

"We all secretly believe we are right about everything and, by extension, we are all wrong." W. Storr, The Unpersuadables
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: 389
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.

Jesse (16 views)
2015-07-29 04:35:27

Riven (37 views)
2015-07-27 16:38:00

Riven (19 views)
2015-07-27 15:35:20

Riven (22 views)
2015-07-27 12:26:13

Riven (12 views)
2015-07-27 12:23:39

BurntPizza (32 views)
2015-07-25 00:14:37

BurntPizza (42 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (31 views)
2015-07-22 22:16:53

NoxInc (20 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!