Game Development Reference
In-Depth Information
In Listing 12-4, we see the task
alertView:clickedButtonAtIndex:
, which is defined by the protocol
UIAlertViewDelegate
and implemented by
RootViewController
. In this task, we first check which
UIAlertView was clicked.
If
alertView
is equal to
pauseGameAlertView
, then we know that the user has clicked the Pause button
on the Game view and is responding to the dialog that is presented. If the user clicks Yes on the
dialog (
buttonIndex == 0
), then the user wants to exit the current game. We call
endOfGameCleanup
and
popViewControllerAnimated:
; the latter, of course, returns us to the Start view. The task
endOfGameCleanup
is responsible for reporting on the social media services, and we will look at
it shortly. If the user had clicked No, we simply unpause the game by calling
setIsPaused
: and
beltCommanderController
and pass in
NO
.
In Listing 12-4, there are two possible
UIAlertViews
that might be responsible for
alertView:clickedButtonAtIndex:
being called. We looked at the case for the
UIAlertView
pauseGameAlertView
; the other option is that the
newGameAlertView
is responsible. This
UIAlertView
is displayed at the end of a game when the user's ship has lost all of its health. This dialog allows the
user to simply start a new game or return to the Start view. As can be seen, starting a new game is
as simple as calling
doNewGame:
on
beltCommanderController
. To return to the Start view, we use the
now familiar task
popViewControllerAnimated:
.
The code responsible for displaying the two different
UIAlertView
is very similar; let's just look at one
of those to understand it. The code that displays the
UIAlertVIew
at the end of a game is shown in
Listing 12-5.
Listing 12-5. RootViewController.m (gameOver:)
-(void)gameOver:(BeltCommanderController*)aBeltCommanderController{
if (newGameAlertView == nil){
newGameAlertView = [[UIAlertView alloc] initWithTitle:@"Your Game Is
Over." message:@"Play Again?" delegate:self cancelButtonTitle:@"Yes"
otherButtonTitles:@"No", nil];
}
[newGameAlertView show];
[self endOfGameCleanup];
}
In Listing 12-5, we see the task
gameOver:
as implemented by
RootViewController
. The task
gameOver:
is defined by the protocol
BeltCommanderDelegate
, which is the delegate protocol used
to communicate between the
RootViewController
and the
BeltCommanderController
when a game
starts or stops. That is to say, the
RootViewController
is a delegate of
beltCommanderView
. This
relationship is defined in the XIB file. In this case, we are looking at the code that is called when the
game is over. In this task, we lazily create the
UIAlertView newGameAlertView
, specifying the text to
be displayed and that
self
should be used as the delegate. To display the
UIAlertView
we call
show
,
causing the view to pop up on the user's screen. The last thing we do is call
endOfGameCleanup
, as
shown in Listing 12-6.