There it is:
main loop
public void gameLoop() {
long lastTicks, currentTicks, elapsedMs, ticksPerSecond;
AdvancedTimer timer = new AdvancedTimer();
ticksPerSecond = AdvancedTimer.getTicksPerSecond();
long sleepTime = AdvancedTimer.getTicksPerSecond() / FPS;
long ticks = 0;
timer.start();
lastTicks = timer.getClockTicks();
while (isRunning) {
currentTicks = timer.getClockTicks();
elapsedMs = ((currentTicks - lastTicks) * 1000) / ticksPerSecond;
fps++;
lastFpsTime += elapsedMs;
// Update fps counter if 1 sec has passed.
if(lastFpsTime >= 1000) {
this.setTitle("FPS=" + String.valueOf(fps));
fps=0;
lastFpsTime =0 ;
}
Graphics g = this.strategy.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, 800, 600);
// move/update anim and draw the sprites
for (int i = 0; i < this.sprites.size(); i++) {
Sprite sprite = (Sprite) sprites.get(i);
sprite.move(elapsedMs);
sprite.draw(g);
}
lastTicks = currentTicks;
timer.sleepUntil(ticks + sleepTime);
ticks += sleepTime;
}
timer.stop();
System.exit(0);
}
move method of class Alien that extends Sprite
public void move(long pDelta) {
this.animation.animate(pDelta);
if ((dx < 0) && (x < 10)) {
this.game.updateLogic();
}
if ((dx > 0) && (x > 750)) {
this.game.updateLogic();
}
// call move on Sprite class
super.move(pDelta);
}
This is the code of the move method on Sprite:
// dx is about 100ms
public void move(long pDelta) {
this.x += ((pDelta * dx) / 1000);
this.y += ((pDelta * dy) / 1000);
}
And this is the animate method of the Animation class. Here that the next frame
of the sprite is determined.
// each frame takes 250ms(frameDuration)
public void animate(long pDelta) {
this.increaseLastFrameChangeBy(pDelta);
if (this.getLastFrameChange() > this.getFrameDuration()) {
this.setLastFrameChange(0);
this.increaseFrameNumber();
if (this.getFrameNumber() >= this.getTotalFrames()) {
this.setFrameNumber(0);
}
}
}
public void increaseLastFrameChangeBy(long pDelta) {
this.lastFrameChange += pDelta;
}
public void increaseFrameNumber() {
this.frameNumber++;
}
public void draw(Graphics g, int pX, int pY) {
g.drawImage(this.frames[this.frameNumber], pX, pY, null);
}
public void setFrameDuration(long frameDuration) {
this.frameDuration = frameDuration;
}