An integral part of all programming, game, or otherwise is the use of Boolean values. Boolean values typically return either true
or false
values, yes
or no
, or 0
or 1
. In Swift, this is the job of the Bool
class of objects. The use of the function .isEmpty()
in our past collection data type examples returns a Boolean value of true
or false
based on whether that collection is empty or not.
In game development, one way we could use Boolean values is to have a global variable (a variable accessible in scope throughout our game/app) that checks if the game is over.
var isGameOver = false
This variable, taken from the PikiPop game, starts the game off with a variable of type bool
named isGameOver
with a starting value of false
. If the events of the game cause this value to change to true
, then this triggers the events associated with the game over state.
However, reading and controlling this type of information about our game, known as the game's state, is best controlled with more than just a single Boolean value. This is because your game and the characters in your game could have various states, such as game over, paused, spawn, idle, running, falling, and more. A special object known as a state machine best manages this type of information. State machines shall be covered in more detail when we discuss the GameplayKit framework.
3.15.237.123