Game Development Reference
In-Depth Information
[label setText:[NSString stringWithFormat:@"%d", [game score]]];
}
for (UILabel* label in remainingTurnsLabels){
[label setText:[NSString stringWithFormat:@"%d", [game remaingTurns]]];
}
}
-(void)scoreIncreases:(CoinsController*)aCoinsController with:(int)newScore{
for (UILabel* label in scoreLabels){
[label setText:[NSString stringWithFormat:@"%d", newScore]];
}
}
-(void)turnsRemainingDecreased:(CoinsController*)aCoinsController with:(int)turnsRemaining{
for (UILabel* label in remainingTurnsLabels){
[label setText:[NSString stringWithFormat:@"%d", turnsRemaining]];
}
[continueButton setHidden:YES];
Score* score = [Score score:[game score] At:[NSDate date]];
[highscoreController addScore:score];
UIWindow* window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
window.rootViewController = nil;
window.rootViewController = self;
[UIView beginAnimations:nil context:@"flipTransitionToBack"];
[UIView setAnimationDuration:1.2];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[coinsController.view removeFromSuperview];
[self.view addSubview:highscoreController.view];
[UIView commitAnimations];
}
Each implementation of the tasks defined by the
CoinsControllerDelegate
protocol for the class
GameController
is shown in Listing 3-7. When a
CoinsController
starts a new game, it calls
gameDidStart:with:
on the
delegate
object. In our case, we want to make sure the remaining turns
label and the score label reflect the starting values of the game. These could be just about anything,
because we don't know if we are starting a new game or continuing an old game. Similarly, these
labels are updated whenever the tasks
scoreIncreases:with:
and
turnsRemainingDecreased:with:
are called.
Lastly, in Listing 3-7, the tasks
gameOver:with:
is called when the game is over. In this task, we
record the high score and switch to the High Score view. Because we allow the game to be played
in landscape or portrait orientation, the phone might be in landscape when the game is over. If
that is the case, we want to make sure the interface orientation is correct, so the High Score view
is displayed correctly. One way to do this is to simply reset our
UIWindow
's
rootViewController
property. This causes
shouldAutorotateToInterfaceOrientation:
to be called and makes sure our
views are shown in the correct orientation.