Domanda

I am using this library to report scores and achievements in my app: https://github.com/csddavies/DDGameKitHelper

So in my app I report percentages to let the user know how close they are to completing an achievement. So I do something like this:

[[DDGameKitHelper sharedGameKitHelper] reportAchievement:@"Achievement1" percentComplete:totalTime/300.0f];

However it seems to be reporting that I completed the achievement, "Achievement1", even though I didn't. When I NSLog totalTime/300.0f I get a value like 0.563 and NOT 100.

So now I am thinking something must be wrong with the reportAchievement method, so I looked at it and I am not sure what could be wrong. Here are the methods related to reportAchievement:

-(void) reportAchievement:(NSString*)identifier percentComplete:(float)percent
{
    if (isGameCenterAvailable == NO)
        return;

    GKAchievement* achievement = [self getAchievement:identifier];
    if (achievement.percentComplete < percent)
    {
        NSLog(@"new achievement %@ reported", achievement.identifier);
        achievement.percentComplete = percent;
        [achievement reportAchievementWithCompletionHandler:^(NSError* error)
         {
             [delegate onReportAchievement:(GKAchievement*)achievement];
         }];

        [self saveAchievements];
    }
}

-(GKAchievement*) getAchievement:(NSString*)identifier
{
    GKAchievement* achievement = [achievements objectForKey:identifier];

    if (achievement == nil)
    {
        achievement = [[[GKAchievement alloc] initWithIdentifier:identifier] autorelease];
        [achievements setObject:achievement forKey:achievement.identifier];
    }

    return achievement;
}

So is there anything here that could cause my achievements to be reported as completed early even though they are really not?

Thanks!

Edit1:

2012-11-19 20:50:15.946 App[16894:907] percent: 0.005556
2012-11-19 20:50:15.947 App[16894:907] achievement percentComplete: 0.004861
2012-11-19 20:50:15.972 App[16894:907] percent: 0.000000
2012-11-19 20:50:15.973 App[16894:907] achievement percentComplete: 0.000000
2012-11-19 20:50:15.974 App[16894:907] percent: 0.026000
2012-11-19 20:50:15.974 App[16894:907] achievement percentComplete: 0.024000
2012-11-19 20:50:16.003 App[16894:907] percent: 0.002600
2012-11-19 20:50:16.004 App[16894:907] achievement percentComplete: 0.002400
2012-11-19 20:50:16.010 App[16894:907] percent: 0.000000
2012-11-19 20:50:16.011 App[16894:907] achievement percentComplete: 0.000000
2012-11-19 20:50:16.012 App[16894:907] percent: 0.016667
2012-11-19 20:50:16.012 App[16894:907] achievement percentComplete: 0.016667
2012-11-19 20:50:16.013 App[16894:907] percent: 0.000000
2012-11-19 20:50:16.014 App[16894:907] achievement percentComplete: 0.000000
È stato utile?

Soluzione

I believe the issue here is that in the check if (achievement.percentComplete < percent) it just is too vague. My percentComplete could be 5% and my percent value could be 6% but in no means is that 100% finished. So with the way the method is above it will just show the achievement banner every time the percentComplete on the achievement is updated which is not the way Game Center should work. I also noticed that even though the banner showed as if I did unlock the achievement, I really didn't when I checked in the Game Center app. So the following method is what I did instead:

-(void) reportAchievement:(NSString*)identifier percentComplete:(float)percent
{
    if (isGameCenterAvailable == NO)
        return;

    GKAchievement* achievement = [self getAchievement:identifier];
    if (percent > achievement.percentComplete)
    {
        NSLog(@"new achievement %@ reported", achievement.identifier);
        achievement.percentComplete = percent;
        [achievement reportAchievementWithCompletionHandler:^(NSError* error) {
             if (achievement.isCompleted) {
                [delegate onReportAchievement:(GKAchievement*)achievement];
             }
         }];

        [self saveAchievements];
    }
}

In this code it will still report the updated value towards the 100% but it will only show a banner if the isComplete BOOL from GC is returned YES.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top