Game Development Reference
In-Depth Information
[self.window makeKeyAndVisible];
return YES;
}
In Listing 3-20, we are looking at the task that is called when our application is fully loaded and
ready to go. You will recall the last two lines from the previous chapter: they put the
UIWindow
on the
screen and return that everything is okay. The earlier part of the task is concerned with unarchiving
our game state. Using the
gameArcivePath
, we call
unarchiveObjectFromFile
on the class
NSKeyedUnarchiver
. This returns our previously archived
CoinsGame
object, if it exists. If this is the
first time the application was run,
existingGame
will be
nil
. Additionally, if an exception occurred, we
set
existingGame
to
nil
as well, because we know that calling
setPreviousGame
on
gameController
can handle a
nil
value. To complete the cycle, let's take a look at
GameController
and see what it
does with this unarchived
CoinsGame, as shown
in Listing 3-21.
Listing 3-21. GameController.m(setPreviousGame:)
-(void)setPreviousGame:(CoinsGame*)aCoinsGame{
previousGame = [aCoinsGame retain];
if (previousGame != nil && [previousGame remaingTurns]>0){
[continueButton setHidden:NO];
} else {
[continueButton setHidden:YES];
}
}
In Listing 3-21, we see that the passed in
CoinsGame
is saved as the local field
previousGame
and retained. If
previousGame
is not
nil
and there are turns remaining, we display the button
continueButton
; otherwise, we hide it. Once the user is shown the Welcome view, they will be able
to continue playing the game where they left off.
Summary
In this chapter, we explored supporting elements required by a complete game. These included
establishing a flow of an application and managing the views that constitute this flow. We looked at
a way to coordinate the game behaviors, such as a change in score, or the end of a game with the
rest of the application using the delegate pattern. We also looked at two ways of persisting data:
through the user settings or by storing an object on disk. Both techniques use the same archiving
and unarchiving techniques provided by the protocol
NSCoding
.