Show the button on game over
We can show and hide the button in the Game
update
method by checking on each frame if the game is over and if the button is present, ensuring that we only show or hide it once, and that would probably work, but I think you can sense the spaghetti code beginning to form if we do that. In general, it's best to avoid too much conditional logic in update
, as it gets confusing and allows for logic bugs. Instead, we can think of every conditional check that looks like if (state_is_true)
as two different states of the system. So, if the new game button is shown, that's one game state, and if it isn't, that's another game state. You know what that means – it's time for a state machine.
A state machine review
In Chapter 4, Managing Animations with State Machines, we converted RHB to a state machine in order to make it change animations on events easily and, more importantly, correctly. For instance, when we wanted RHB to jump...