O'Reilly logo

Beginning Android Tablet Games Programming by Jeremy Kerfs

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing Timing and Complex Motion

Now you can move on to create a system that enables you to accurately set how quickly your game runs. No longer will the game loop be subject to the whims of the device. To do this, you use a timer and then adjust the movement based on how much time has elapsed. This means that if one cycle takes a very long time and another takes a short amount of time, the sprite will move a certain distance depending on that value. Looking at the code is the best way to understand this type of method. Follow these steps:

  1. Replace the code in the synchronized block in GameLogic.java with the code in Listing 2-13.

    Listing 2-13. Testing a Constant FPS Game

    try { Thread.sleep(30); } catch (InterruptedException e1) { } ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required