Implementing the game loop with a thread
Now we have learned about the game loop, threads, and try
and catch
, we can put it all together to implement our game loop.
We will add the entire code for the game loop, including writing two methods in the PongGame
class to start and stop the thread that will control the loop.
After we have done this, we will again need to do a little bit more theory. The reason for this is that the player can quit the app whenever they like, and our game's thread will need to know so that it can stop itself. We will examine the Android activity lifecycle, which will give us the final pieces of the puzzle that we need before we run our game.
Implementing Runnable and providing the run method
Update the class declaration by implementing Runnable
, just like we discussed we would need to and as shown in this next highlighted code:
class PongGame extends SurfaceView implements Runnable{
Notice that we have a new error in the code. Hover...