Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (616)
Games in Android Showcase (173)
games submitted by our members
Games in WIP (659)
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  
  FPS and UPS / General Animation Looping  (Read 2542 times)
0 Members and 1 Guest are viewing this topic.
Offline Zenon

Innocent Bystander

« Posted 2010-12-21 21:42:44 »

After reading Java for Dummies and understanding the fundamentals of programming, I've wanted to move onto to game making. After a bit of looking around for tutorials, I have found that there are 20,000 ways to program the animation loop, some work, some don't, some suck, others don't, and I can't quite decide what's going to be best for me. I've read some tutorials, but with everyone disagreeing on the subject, it's hard to form a basis. So how do people on this forum program the basic animation loop for their games?
Offline Gudradain
« Reply #1 - Posted 2010-12-21 22:22:14 »

Mostly with a
     //execute your game code here

Here is an example : Basic Game
Offline dbotha

Senior Newbie

« Reply #2 - Posted 2010-12-21 22:27:59 »

Take a look at this article if you haven't already, it will give you a good starting point.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2010-12-22 09:45:56 »

The following is from the article link in the previous post.

example code of the game loop in it’s most simplest form:

    bool game_is_running = true;

    while( game_is_running ) {

The problem with this simple loop is that it doesn’t handle time, the game just runs.

I'm in a minority, but I think an entirely reasonable alternative is to consider using a single java.util.Timer to run the game loop. It's not that hard to set up an "Observer" design pattern to subscribe and unsubscribe all objects or processes that need animation ticks. I started an example in the Tutorials section but haven't had time to make the revisions to put it in an easier-to-read format. One big plus: new loops start at regular intervals without any need to further manage the amount of code that runs within each animation cycle (as long as you don't try to animate 10,000 objects and start overshooting the cycle time). Seems to scale well.

If you don't want to deal with the Observer pattern, this is even simpler, and gives a steady 40 updates a second (well actually a little less, but still reasonably steady):
Timer timer = new Timer();
timer.schedule(new RemindTask(), 25, 25);
class RemindTask() extends TimerTask {
   public void Run() {

It's not that hard to set it up so that you can turn the Timer on or off as needed. And the repaints occur often enough you probably won't need to add any more of them to the response code for the various listeners.

music and music apps:
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Coldstream24 (18 views)
2015-09-03 00:41:28

Andrew_3ds (30 views)
2015-09-01 19:08:10

afikri (20 views)
2015-08-31 09:30:22

afikri (27 views)
2015-08-31 09:30:07

afikri (15 views)
2015-08-31 09:27:24

afikri (17 views)
2015-08-31 09:26:40

Roquen (32 views)
2015-08-29 11:30:54

GamerC4 (38 views)
2015-08-22 20:38:50

GamerC4 (36 views)
2015-08-22 20:37:18

GamerC4 (42 views)
2015-08-22 20:37:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

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