开发者

Java 2D game - Timers vs Threads?

Hey guys i'm making my fi开发者_C百科rst 2D Java game and I am currently using Timers to control and update everything. I was wondering what method was best for a 2D Java game, Timers or Threads? currently I have a Timer in my gameCanvas class which renders/draws everything aswell as checking collision. I Then also have a Timer for my player, which is used for moving him.

The only reason I used timers is because I'm new to Java Programming and it seemed the easiest way at the time considering all I had to put was this:

Timer timer = new Timer(5,this);
timer.start();

What would you guys recommend?


What pst says about the pros and cons of the two approaches is absolutely correct.

However, I wouldn't recommend either. I would use a "designed for games" game engine such as Slick2D, and it will help you immensely with the update and logic in a game, the structure of the main game loop, as well at take care of a lot of the low-level issues you'll run into.

If you're considering using Swing for games, I would also stay far away from that, for the reasons outlined in my answer here.

Slick2D comes pre-built to handle the timing issues, and figuring out the deltas between rendering frames, etc. You also get a lot of nice stuff that you don't have to build on your own, such as input handling (keyboard, mouse, controllers), accelerated graphics (your games will automatically benefit from graphics acceleration because the engine takes care of this for you), and when you get ready for audio it also has a great audio library.

Writing all this stuff yourself is going to make you want to cry. Unless you're really into building the engine itself (as opposed to wanting to spend your time building the game logic itself), building engines is a lot of time spent when you could just use a mature tool off-the-shelf.


It depends on the type of game.

For a "fast game" (e.g. a shooter) a dedicate game loop (perhaps in a thread) with a yield/wait (as appropriate) will generally function best. For a "slow games" timers will work just fine -- there are however, two types of Timers:

  1. a "Swing" timer (javax.swing.Timer)
    1. advantage: runs on the EDT so can touch Swing/AWT objects
    2. disadvantage: runs on the EDT so has poor resolution and timing guarantees
  2. a "service" timer (java.util.Timer)
    1. advantages:
      1. supports "fixed rate" timer
      2. runs on own thread (so resolution/consistency is better, but not guaranteed)
    2. disadvantages: does not run on the EDT (so can't touch Swing/AWT).

In many cases the game must still compute the "delta" between when the last event occured and when the current event occurred and use this in calculations. Without a model that uses a delta, game speed will vary between systems.

Personally, I would recommend LWJGL: Lightweight Java Game Library which offers a high-resolution clock well suited for "fast game" loops. It also supports "direct" OpenGL bindings ;-)

I do not have any experience developing "slow games".

Happy coding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜