开发者

Unlock Achievement - GameCenter iPhone

开发者 https://www.devze.com 2023-01-31 01:10 出处:网络
I am trying to unlock an achievement in the game i am making for the iPhone but being rather unsuccessful.

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.


    - (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%


Btw, the "completed" property isn't always set to YES if you set percentComplete=100, at least not within the same session. I spent a while debugging why my game awarded achievements several times even when percentComplete got set to 100.

0

精彩评论

暂无评论...
验证码 换一张
取 消