Friday, January 23, 2015

Anansi: The Game Loop - Revisited


We decided to go with the AnimationTimer. It runs smooth at fixed 60fps and does what we want.

It should be possible for a game loop to

  • start
  • stop
  • pause
  • resume

The AnimationTimer doesn't have a pause flag, so we'll add a simple boolean to indicate the state.

Here's what we need:

private AnimationTimer gameLoop;
private boolean gamePaused = false;
...
private void startGame() {
 
      gameLoop.start();
      
}

private void pauseGame() {

 gamePaused = true;
 
 gameLoop.stop();
 
}

private void resumeGame() {

 gamePaused = false;

 gameLoop.start();
 
}

private void stopGame() {
 
 // TODO: remove event handlers (player, etc)
 
}

private void createGameLoop() {
 
  gameLoop = new AnimationTimer() {
   
      @Override
      public void handle(long l) {
       
        // player AI (input)
       
        // sprite AI

        // add sprites (clouds, enemies, bullets, missiles)

        // move sprites internally

        // move sprites in the UI

        // check if sprites can be removed (eg collsion, off-screen)

        // update debug information    

      }

  };
      
} 

That's about it. We'll add other parts like sprite collision once the basic engine is complete.

No comments:

Post a Comment