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) { } ...

Get Beginning Android Tablet Games Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.