6.8.14. CannonThread: Using a Thread to Create a Game Loop
Figure 6.20 defines a subclass of Thread
which updates the game. The thread maintains a reference to the SurfaceView
’s SurfaceHolder
(line 538) and a boolean
indicating whether the thread is running. The class’s run
method (lines 556–587) drives the frame-by-frame animations—this is known as the game loop. Each update of the game elements on the screen is performed based on the number of milliseconds that have passed since the last update. Line 559 gets the system’s current time in milliseconds when the thread begins running. Lines 561–586 loop until threadIsRunning
is false
.
535 // Thread subclass to control the game loop536 private class CannonThread ...
Get Android™ How to Program, Second Edition 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.