Game Development Reference
In-Depth Information
Inside the block object, the achievements member variable is either allocated or
has all objects removed from it. This allows you to call the loadAchievements
method at a later time to refresh the list of achievements. The returned array
loadedAchievements contains a number of GKAchievement instances, which
are then transferred to the achievements NSMutableDictionary simply for
ease of access. The NSDictionary class lets you retrieve an achievement by its
string identifier directly instead of having to iterate over the array and comparing each
achievement's identifier along the way. You can see this in the getAchieve-
mentByID method in Listing 14-17 .
Listing 14-17 . Getting and Optionally Creating an Achievement
-(GKAchievement*) getAchievementByID:(NSString*)identifier
if (isGameCenterAvailable == NO)
GKAchievement* achievement = [achievements objectForKey:identifier];
if (achievement == nil)
// Create a new achievement object
achievement = [[GKAchievement alloc] initWithIdentifier:identifier];
[achievements setObject:achievement forKey:achievement.identifier];
return achievement;
This is where you need to be careful. The getAchievementByID method creates a
new achievement if it can't find one with the given identifier, assuming that this
achievement's progress has never been reported to Game Center before. The
loadAchievements method in Listing 14-16 only obtains achievements that have
been reported to Game Center at least once. For any other achievement, you have to
create it first. So, getAchievementsByID will always return a valid achievement
object, but you'll only notice whether that achievement is really set up for your game
when you try to report its progress to Game Center.
Search Nedrilad ::

Custom Search