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 new error in the code. Hover the mouse cursor over the word Runnable
, and you will see a message informing you that we need to implement the run
function just as we discussed during the discussion on interfaces and threads in the...