[java] How to control animation speed in Java Applet?

Started by
0 comments, last by CaptainJester 16 years, 11 months ago
I'm pretty new to Java, and I've been making a couple simple Applet games to get my feet wet. I made a Pac Man game and a Super Mario game. I am controlling the game by running a big infinite loop inside the Run() method, then waiting a few milliseconds at the end with a thread.sleep() command. It gets the job done.. it will run through and update everything then redraw the screen and everything looks okay. The problem with doing it that way is that the game runs at a different speed on any given computer. Older computers will take longer to run through all the update and redraw commands, so the game runs in slow motion. I know there has to be a less-sloppy way to control the game's speed. I think I just need to separate the delay timer from the update commands so that the speed is controlled entirely by the computer's clock instead of the processing speed followed by the clock. Does that make sense?
Advertisement
There are 2 ways to do movement and animation in games. One is frame based, which you are using. The other is time based, which you need. If you google for time based movement, you should be able to find some tutorials. There is also an article here on GameDev.

http://www.gamedev.net/reference/articles/article1604.asp
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement