2

我已经为我的游戏建立了目标,一切正常,接受在完成任何目标后不再调用目标的部分。

我知道有一个GKAchievement“已完成”类的属性,它是一个布尔值,当成就 100% 完成时返回是。

这是在成就 100% 完成时调用的方法,它传递成就标识符 id 并报告 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!");
}   }

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

但它总是有效......关于如何处理这个问题有更好的主意吗?

4

2 回答 2

1

在界面中添加变量和属性:

NSMutableDictionary *earnedAchievementCache;

@property (nonatomic, retain)NSMutableDictionary *earnedAchievementCache;

以 .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");

         }];
    }
}


}

在 dealloc 中:

[self.earnedAchievementCache release];

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

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

于 2012-07-09T04:39:13.663 回答
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];

}];

}

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

于 2012-07-08T23:15:54.197 回答