Game Development Reference
In-Depth Information
[self removeFromParent];
[_mainMenuButtons show];
}
@end
In
didLoadFromCCB
, the volume values are assigned to the slider values. Coincident-
ally, both
sliderValue
and volumes use the same value range between 0.0 and 1.0.
Initially, the
GameState
volumes will return 1.0, which will position both volume slider
handles to the far right. Whenever the
volumeDidChange:
method runs, the received
sender is compared with the existing ivars in order to determine whether it was the music
or the effects volume that changed. In both cases, the corresponding
GameState
prop-
erty is assigned the current
sliderValue
.
The
shouldClose
method now also calls the
NSUserDefaults
method
syn-
chronize
to ensure the data in
GameState
is persisted to disk when the settings pop-
over is closed. This is just a precaution.
NSUserDefaults
does synchronize its data
periodically, but it may not do so soon enough if the app were to crash shortly after clos-
ing the settings popover—or if you hit the stop button in Xcode.
Note
You certainly should not call
synchronize
every time an
NSUser-
Defaults
value changes, because disk access is a comparatively slow opera-
tion. And like I said before about separation of concerns: should a developer
really concern himself with the fact that synchronizing
NSUserDefaults
is
required to write the values in
GameState
to disk? You can certainly improve
this by adding a corresponding
synchronize
method to
GameState
and
call that instead.
If you run the project now and open the settings menu, you'll notice the slider handles
start out at the far right. Drag the slider handles, close the settings popover, and then quit
the app or hit the stop button in Xcode.
You can then relaunch the app from Xcode, and you'll see the slider positions (and thus
their values) will have been persisted. You can also try hitting the stop button in Xcode
while the settings popover is still open, and any changes to the sliders will most likely
have been reverted to their previous state on the next relaunch.
Search WWH ::
Custom Search