O'Reilly logo

Android Game Recipes: A Problem-Solution Approach by J. F. DiMarzio

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

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() ...

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