Dear JGO Members,
I’m having this weird problem when doing side scrolling at a fast speed, the rectangles drew with g.fillRec() start to have some sort of silhouette and images flashes, I have tried to use variable framerate game loop or increase fps in the fixed framerate loop but neither works. The following is an attempt to demonstrate the problem:
Flashing when moving 1
Flashing when moving 2
Picture to show where the silhouette is when scrolling:
http://img340.imageshack.us/img340/553/text3616.png
Uploaded with ImageShack.us
I’m not exactly sure what the cause of the problem is since it looks fine if I scroll at slower speed, I’m guess the problem has to do with the game loop I’m using, note that I want to have a fixed framerate game loop. Below is the code of the loop, thanks in advance!
The loop is taken from here:
Fixed Framerate loop
http://wiki.games4j.com/wiki/en/Timing_in_main_loops
private int fps;
private long timeThen;
/** Set FPS */
private void setFPS() {
fps = 60;
timeThen = System.nanoTime();
}
/** Timer */
public void sync() {
long gapTo = 1000000000L / fps + timeThen;
long timeNow = System.nanoTime();
while (gapTo > timeNow) {
try { Thread.sleep(1);
} catch (InterruptedException e) {}
timeNow = System.nanoTime();
}
timeThen = timeNow;
}
/** Fixed Framerate Game loop */
private void gameLoop() {
long currentUpdateTime = System.nanoTime();
long lastUpdateTime;
while(gameRunning) {
lastUpdateTime = currentUpdateTime;
currentUpdateTime = System.nanoTime();
long elapsedTime = (currentUpdateTime - lastUpdateTime)/(1000*1000);
/** Update Game */
gameUpdate(elapsedTime);
/** Render */
gameRender();
/** Run timer */
sync();
}
}