Game Development Reference
In-Depth Information
Figure 7-13 . The Main Scene with background and Settings menu
Persisting Game States
Since you'll want the app settings to persist across launches, and because you'll soon need
to persist other data such as unlocked levels, it's time to introduce the GameState class
that persists the game's various states.
Introducing the GameState Class
The GameState class will be a wrapper for NSUserDefaults , in order to avoid in-
jecting the same code with the same string keys all over the code. Just like NSUserDe-
faults , it will be a singleton class. A singleton is a class that can be instantiated only
once.
Did I hear you think, “Finally!”? Or was it “Eeww!?!”? I am aware that singletons are a
hotly debated topic—some people even have extremely subjective opinions about it.
Singletons are frequently overused and/or misused, and especially for beginning program-
mers they are tempting constructs since the global accessibility makes them super-simple
workarounds for passing along data and references between objects.
I just have one plea: before you use a singleton, make sure you have exhausted all other
options or dismissed them as impractical. Only relatively few variables and methods need
to be global.
Search WWH ::




Custom Search