Game Development Reference
In-Depth Information
Figure 9-11.
First achievement in the Game Center app
In Figure
9-11
, we see a screenshot from the Game Center app displaying the achievements for Belt
Commander. We can see that the achievement in this view was awarded. Listing 9-5 shows the code
that awards the achievement.
Listing 9-5. BeltCommanderController.m (checkAchievements)
-(void)checkAchievements{
if (asteroids_destroyed >= 10){
GKAchievement* achievement = [[GKAchievement alloc]
initWithIdentifier:@"beltcommander.10asteroids"];
achievement.percentComplete = 100.0;
[achievement reportAchievementWithCompletionHandler:^(NSError *error) {
if (error){
// report error
}
}];
}
}
In Listing 9-5, we see the task
checkAchievements
from the class
BeltCommanderController
,
which is responsible for handling the in-play game logic. The first thing we do is check if the
asteroids_destroyed
is bigger or equal to 10. The variable
asteroids_destroyed
is simply
incremented whenever an asteroid in the game is destroyed. If
asteroids_destroyed
is at least 10,