Game Development Reference
In-Depth Information
Figure 9-10.
A leaderboard with a sample score
In Figure
9-10
, we see a score displayed on a leaderboard. There is only one score at the moment,
because the screenshot was taken during development of the game. At the top, we can see three
options for viewing high scores: Today, This Week, or All Time. The component that is displaying the
high scores is a pre-built component called
GKLeaderboardViewController
that comes with GameKit;
the code to bring up this component is shown in Listing 9-3.
Listing 9-3. RootViewController.m (leaderBoardClicked:)
- (IBAction)leaderBoardClicked:(id)sender {
GKLeaderboardViewController* leaderBoardController =
[[GKLeaderboardViewController alloc] init];
leaderBoardController.category = @"beltcommander.highscores";
leaderBoardController.leaderboardDelegate = self;
[self presentModalViewController:leaderBoardController animated:YES];
}
In Listing 9-3, we see the task
leaderBoardClicked:
, which is called when the user clicks
the Leaderboard button. This class is called on an instance of
RootViewController
, which
is the main
UIViewController
for our example game Belt Commander. We don't have to
know much about
RootViewController
except that it is a
UIViewContoller
and conforms to
GKLeaderboardViewControllerDelegate
. To display the view shown in Figure
9-10
, first create a
GKLeaderboardView
and set the
category
property to the ID of our leaderboard. Then call