Game Development Reference
In-Depth Information
In Figure 9-9 , we see the result of calling GameKit's authentication task. The user is prompted to
sign in to Game Center with an existing account, create a new account, or cancel. If the user cancels
enough times, the application will no longer display this dialog. The exact number of times that the
user must cancel before this happens seems to change with different releases. With iOS 5,
it is approximately three times. Once the user has successfully authenticated, the application is
now connected to Game Center for that user account. When the application is run again, calls to
authenticateWithCompletionHandler : will call the handler code without pestering the user to sign in
again. The user will see something like Figure 9-2 .
Once Game Center is enabled, we will want to start taking advantage of its features. The following
section looks at how to report a score to a leaderboard.
RootViewController.m (notifyGameCenter)
if ([localPlayer isAuthenticated]){
GKScore* score = [[GKScore alloc] initWithCategory:@"beltcommander.highscores"];
score.value = ([beltCommanderController score];
[score reportScoreWithCompletionHandler:^(NSError *error){
if (error){
//handle error
}
}];
}
}
In Listing 9-2, we see the task notifyGameCenter , which is called every time a game ends. In
this task, we check to see if the localPlayer is authenticated. If so, we simply create a GKScore
object, specifying the ID of the leaderboard to which we are submitting the score—in this case,
beltcommander.highscores .” Once we have a reference to the GKScore object, we set the score
property to the score from the last game. The property score can by any type of number—in this
case, it is a long . Lastly, we call reportScoreWithCompletionHandler : and pass a block to handle any
error. In our example, we don't do anything with the error.
Note If the user is offline or there was some sort of network issue, GameKit will automatically
resubmit scores when it is run in the future, ensuring that a player's hard work is properly recorded.
Figure 9-10 shows a score on the leaderboard.
 
 
Search WWH ::




Custom Search