Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Animation that is not dependent on fps?

    I've looked for information on how to set-up an animation in java. Everything I find shows how to set up a thread that implements Runnable, uses a while loop, the sleep method, and System.currentTimeMillis() in order to create a steady frame rate.

    Code:
        public void start() {
            animator = new Thread(this);
            animator.start(); // calls run
        }
    
        /* handles frame rate */
        public void run() {
            long tm = System.currentTimeMillis(); // stores what time the program started
            while (Thread.currentThread() == animator) {
                repaint(); // calls update
                try {
                    tm = tm + delay; // tm + delay is when the next update should be.
                    Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
                    // if tm > currentTime, then sleep the difference.
                    // if program starts to lag, then don't sleep, update immediately!
                } catch (Exception e) {
                    break;
                }
                frame++; // everything drawn depends on what frame it is.
            }
        }
    
        public void stop() {
            animator = null;
        }
    The problem with this is that the animation is dependent on what frame it is. However, I have played java games wherein the frame rate might be very high (say over 100 fps, depending on your computer's specs), yet the animations do not play faster even if the frame rate is high.

    And vice versa, the animation is not slowed down if the frame rate is going slow. In this case the animation may be choppy, but it seems like certain parts of the animation are being skipped over in order to have the animation finish within the normal amount of time.

    What is this called? And how is it done?
    Last edited by Croolsby; 12-19-2011 at 04:53 AM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •