Chapter     17

Keeping Time

Some games, perhaps the one you are developing, are time based. This could mean that a specific challenge in a game has a time limit in which it needs to be completed, or that the entire game itself can only be played for a predetermined amount of time.

The solutions in this chapter will help you create a timer within your game. You will then use that timer to write to the screen, and to exit the game action after expiration. The key component for tracking time within a game is the Android class CountDownTimer(). The CountDownTimer() is a very powerful, yet easy to implement, tool.

17.1 Track Time Within the Game

Problem

The user should only have a set amount of time to complete a task.

Solution

Use a CountDownTimer() ...

Get Android Game Recipes: A Problem-Solution Approach 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.