Search code examples
iphonegamekitgame-centerachievementsunlock

Unlock Achievement - GameCenter iPhone


I am trying to unlock an achievement in the game i am making for the iPhone but being rather unsuccessful.

From Apples own GKTapper project sample demonstrating Game Center code I have copied the GameCenterManager.h and .m and the AppSpecificValues.h files into my project. I have successfully got loading the achievements and leaderboards for viewing.

However I can't work out or get right how to actually unlock an achievement. Could some point out how using this or without the GameCenterManager how can I unlock an achievement please?

Thanks.


Solution

  •     - (void) reportAchievementIdentifier: (NSString*) identifier percentComplete: (float) percent
    {
        GKAchievement *achievement = [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];
        if (achievement)
        {
             achievement.percentComplete = percent;
             [achievement reportAchievementWithCompletionHandler:^(NSError *error)
                 {
                      if (error != nil)
                      {
                          // Retain the achievement object and try again later (not shown).
                      }
                 }];
        }
    }
    

    Call this method like this:

    [self reportAchievementIdentifier:indentifier percentComplete:percent];
    

    If you want to just unlock the achievement call this:

    [self reportAchievementIdentifier:indentifier percentComplete:100.0];
    

    You can use the float for calculate the percent of the achievement, and if the user reaches the 100 the achievement gets unlocked.

    You can also do this:

    [self reportAchievementIdentifier:indentifier percentComplete:((actualpoints/neededPoints)*100.0)];
    

    neededPoints means the points you need for unlock this achievement. For example: actualPoints = 300; neededPoints = 600;

    It calculates: 300/600 = 0.5 * 100 = 50%