我怎么知道GK成就是否已经完成?

时间:2012-07-03 11:01:57

标签: iphone ios game-center

我为我的游戏制定了一个目标,一切正常,接受在任何目标完成后不再调用目标的部分。 我知道有一个GKAchievement类的属性“已完成”这是一个布尔值,当成就完成100次时返回yes。

这里是一个方法,当一个成就是100个precent完成时调用它传递id是wich是成就标识符并报告acheeee:

- (void)AchivmentDidAchive:(id)Achivment{
NSString *identifier = Achivment;
NSLog(@"%@",identifier);
self.achivment = [[GKAchievement alloc]initWithIdentifier:identifier];
self.achivment.showsCompletionBanner = YES;


if (!self.achivment.completed) {
    self.achivment.percentComplete = 100;
    NSLog(@"Reproting!");
    [self.achivment reportAchievementWithCompletionHandler: ^(NSError *error)
     {

     }];
}
else {
    NSLog(@"Achivment Completed!");
}   }

我在这里要做的是将完成的precent设置为100并在下次报告它,即希望再次调用。

但它总是有效......对于如何处理这个问题更好的想法?

2 个答案:

答案 0 :(得分:1)

在界面添加变量&属性:

NSMutableDictionary *earnedAchievementCache;

@property (nonatomic, retain)NSMutableDictionary *earnedAchievementCache;

in .m:

@synthesize earnedAchievementCache;

- (void) submitAchievement: (NSString*) identifier percentComplete: (double) percentComplete
{




if(self.earnedAchievementCache == NULL)
{

    [GKAchievement loadAchievementsWithCompletionHandler: ^(NSArray *scores, NSError *error)
     {
         if(error == NULL)
         {

             NSMutableDictionary* tempCache= [NSMutableDictionary dictionaryWithCapacity: [scores count]];
             for (GKAchievement* score in scores)
             {
                 [tempCache setObject: score forKey: score.identifier];
             }
             self.earnedAchievementCache= tempCache;
             [self submitAchievement: identifier percentComplete: percentComplete];
         }


     }];
}
else
{

    GKAchievement* achievement= [self.earnedAchievementCache objectForKey: identifier];
    if(achievement != NULL)
    {
        if((achievement.percentComplete >= 100.0) || (achievement.percentComplete >= percentComplete))
        {


            achievement= NULL;



        }
        achievement.percentComplete= percentComplete;
    }
    else
    {
        achievement= [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];
        achievement.percentComplete= percentComplete;

        [self.earnedAchievementCache setObject: achievement forKey: achievement.identifier];
    }
    if(achievement!= NULL)
    {
        //Submit the Achievement...


        if (achievement.percentComplete>=100) {
           //show banner
            achievement.showsCompletionBanner = YES; //only in IOS 5+
        }



[achievement reportAchievementWithCompletionHandler: ^(NSError *error)
         {
    if (error!=NULL){
            NSLog(@"Error!!");
    } else NSLog(@"all is well");

         }];
    }
}


}

in dealloc:

[self.earnedAchievementCache release];

我正在使用缓存来提交已提交/已完成的分数

PS:代码非常完美,只需将其复制并粘贴到您的课程中即可使用

答案 1 :(得分:0)

这是我在帮助者Game Center课程中使用的内容:

-(void) reportAchievementWithID:(NSString*) AchievementID {

[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {

    if(error) NSLog(@"error");

    for (GKAchievement *ach in achievements) {
        if([ach.identifier isEqualToString:AchievementID]) { //already submitted
            NSLog(@"Already submitted");
            return ;
        }
    }

    GKAchievement *achievementToSend = [[GKAchievement alloc] initWithIdentifier:AchievementID];
    achievementToSend.percentComplete = 100;
    achievementToSend.showsCompletionBanner = YES;
    [achievementToSend reportAchievementWithCompletionHandler:NULL];

}];

}

注意:我没有在成就中使用百分比,所以如果你这样做,你需要稍微修改一下。