Hi!
I’ve made a first little java applet game (not my first game ever though). It’s an simple “Monty on the run”-styled plattform game. The player is supposed to collect discs and avoiding angry monsters. The game has no level scrolling and every level is one screen only.
Logically the game is working just fine, but the game has got some irritating performance issues. The game is running a bit jerky and on computers with Windows Vista the speed is not constant (sometimes it’s running faster and sometimes slower). 
The game is also freezing for short periods now and then.
The game started freezing for even longer periods when I included sound in the game, which I feel is a bit strange since all sound is handled by java.applet.AudioClip. I do not instantiate new audioclips every time they are played. All audioclips used in the game are loaded when the game is starting and stored in a special object.
I am using double buffering and active rendering for my animation. My main loop is looking like this:
long lastSleep = System.nanoTime();
long sleepTime;
while (true) {
if (state == State.RUNNING) {
sven.move(); // Moving the player sprite
checkControls(); // Check keyboard controls
collisions(); // Checking if the player is colliding the enemies
levelElementCollision(); // Checking if the player is colliding with level elements such as walls.
}
if (state == State.RUNNING || state == State.GAMEOVER) {
animateDiscs(); // Animate all the discs the player should collect
handleEnemies(); // Performing enemy "AI" and moving enemies
}
paintScreen(); // Painting active to the screen
sleepTime = 16000000 - (System.nanoTime() - lastSleep);
lastSleep = System.nanoTime();
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime/1000000L);
} catch (InterruptedException ex) {}
}
}
I really do not know what I am doing wrong ???. I’m not instantiating any objects at all during a level and is doing a “System.gc()”-call after each level. I’ve checked the applet with Eclipses profiler and saw that my game was instantiating a LOT of char[] and int[]. I am not instantiating a single one however, so this must be some sort of system operation? Is this normal, or is it a result of me doing something terribly wrong? ???
I am having around 8-10 sprites on screen at once. All level graphics (walls, floor etc.) are drawn to the background at the start of a new level.
I know that some things in the game are not perfectly optimized, for example the collision detection and the paint method (including a lot of list iterations). However, even if I bybass the collision, or most of the paint-method, the game is still running jerky and freezy at the same level as before. :-\
I really want my game to work, but do not know what to do. What could cause these problems? You are my only hope!