Implementing the game loop with a thread

Now that we have learned about the game loop and threads, we can put it all together to implement our game loop in the Living Drawing project.

We will add the entire code for the game loop, including writing code in two functions in the MainActivity class to start and stop the thread that will control the loop.

Tip

Reader challenge

Can you work out for yourself how the Activity-based class will start and stop the thread in the LiveDrawingView class?

Implementing Runnable and providing the run function

Update the class declaration by implementing Runnable, as shown in the following highlighted code:

class LiveDrawingView(
        context: Context,
        screenX: Int)
    : SurfaceView(context), Runnable {

Notice that we have a ...

Get Android Programming with Kotlin for Beginners 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.