4. The Game Loop

One of the most important elements of a game is the game loop. It is the heartbeat that keeps the game ticking. Every game has a series of tasks it must perform on a regular basis, as follows:

• Update the game state

• Update the position of game entities

• Update the AI of game entities

• Process user input, such as touches or the accelerometer

• Play background music and sounds

This may sound like a lot, especially for a complex game that has many game elements, and it can take some time to process each of these stages. For this reason, the game loop not only makes sure that these steps take place, but it also ensures that the game runs at a constant speed.

This chapter shows you how to build the game loop for Sir Lamorak’s ...

Get Learning iOS Game 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.