GameKit achievement questions

旧街凉风 提交于 2019-12-25 14:46:07

问题


My game center achievements are appearing in Game Center, so I know my implementation is correct in reporting.

Couple of questions on that.

First, in Game Center it is not showing the percentage view on the image... ie 2% complete next to the achievement, even though I have reported .02. I know the achievement is being reported because if I throw the 100 at it, it records the achievement.

Second, my achievements are not appearing to the user upon reward. As I understood, this functionality was suppose to be handled automatically by gamekit. I was under the impression the the small modal would appear letting the user know they completed an achievement. I now think there is something I have to do, because no small modal is appearing.

I will attach my code, but most of it stock.

My last problem is retrieving scores. I believe I am going to have to store my own scores because my current implementation does not look like it will mesh well.

Thanks in advance...

- (void) loadAchievements
{    [GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements,        NSError *error) {
if (error != nil)
{
    // handle errors
}
if (achievements != nil)
{
    // process the array of achievements.
}
}];
}

-(float)getAchievementPercentageForIdentifier:(NSString *)identifier {

__block float percentage = 0;

[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {
    if (error != nil)
    {
        // handle errors
    }
    if (achievements != nil)
    {
        // process the array of achievements.

        for (GKAchievement *achievement in achievements) {

            if ([achievement.identifier isEqualToString:identifier]) {

                percentage = achievement.percentComplete;
                NSLog(@"percent complete --> %f", achievement.percentComplete);

            }

        }

    }
}];

NSLog(@"Percentage --> %f", percentage);

return percentage;

}

- (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).
         }
     }];
}
 }

 -(void) addCompletedGameToAchievements {

float oneGamePercentage = 0;
float tenGamePercentage = 0;
float fiftyGamePercentage = 0;
float hundredGamePercentage = 0;
float fivehundredGamePercentage = 0;
float thousandGamePercentage = 0;

int gamesComplete = 0;

oneGamePercentage = [self getAchievementPercentageForIdentifier:kAchievementGamesCompletedOne];
tenGamePercentage = [self getAchievementPercentageForIdentifier:kAchievementGamesCompletedTen];
fiftyGamePercentage = [self getAchievementPercentageForIdentifier:kAchievementGamesCompletedFifty];
hundredGamePercentage = [self getAchievementPercentageForIdentifier:kAchievementGamesCompletedHundred];
fivehundredGamePercentage = [self getAchievementPercentageForIdentifier:kAchievementGamesCompletedFivehundred];
thousandGamePercentage = [self getAchievementPercentageForIdentifier:kAchievementGamesCompletedThousand];

if (oneGamePercentage != 100) {

    [self reportAchievementIdentifier:kAchievementGamesCompletedOne percentComplete:100];

}

if (tenGamePercentage != 100) {

    gamesComplete = tenGamePercentage * 10;
    gamesComplete++;

    [self reportAchievementIdentifier:kAchievementGamesCompletedTen percentComplete:(gamesComplete * .10)];

}

if (fiftyGamePercentage != 100) {

    gamesComplete = fiftyGamePercentage * 50;
    gamesComplete++;


    NSLog(@"fifty game reported %f ",(gamesComplete * .02));
    [self reportAchievementIdentifier:kAchievementGamesCompletedFifty percentComplete:(gamesComplete * .02)];

}

if (hundredGamePercentage != 100) {

    gamesComplete = hundredGamePercentage * 100;
    gamesComplete++;

    [self reportAchievementIdentifier:kAchievementGamesCompletedHundred percentComplete:(gamesComplete * .01)];

}

if (fivehundredGamePercentage != 100) {

    gamesComplete = fivehundredGamePercentage * 500;
    gamesComplete++;

    [self reportAchievementIdentifier:kAchievementGamesCompletedFivehundred percentComplete:(gamesComplete * .002)];

}

if (fivehundredGamePercentage != 100) {

    gamesComplete = thousandGamePercentage * 1000;
    gamesComplete++;

    [self reportAchievementIdentifier:kAchievementGamesCompletedThousand percentComplete:(gamesComplete * .0001)];

}

NSLog(@"100 game percentage -- > %f", hundredGamePercentage);

}

回答1:


Many problems...

  • I'm confused by the logic behind incrementing gamesComplete
  • You should store them locally instead of asking GameCenter every time.
  • -getAchievementPercentageForIdentifier: will always return 0 because GameKit methods are asynchronous.
  • GKAchievement.percentageComplete is a percentage. You need to multiply by 100.

I think you have to do your own achievement notifications.



来源:https://stackoverflow.com/questions/6500752/gamekit-achievement-questions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!