2

The Game Center tutorial I was following showed me everything except how to report the scores and achievements to the leaderboards.

This is the score part:

+ (void)reportScore:(Float64) score forIdentifier: (NSString*) identifier {
GKScore* highScore = [[GKScore alloc] initWithLeaderboardIdentifier:@"Tap_Competition_LB"];
highScore.value = score;
[GKScore reportScores:@[highScore] withCompletionHandler:^(NSError *error) {
    if (error) {
        NSLog(@"Error in reporting scores: %@", error);
    }
}];
}

and this is the achievement part:

+ (void) reportAchievementIdentifier: (NSString*) identifier percentComplete: (float) percent
{
    GKAchievement *achievement = [[GKAchievement alloc] initWithIdentifier: identifier];
    if (achievement)
    {
        achievement.percentComplete = percent;
        achievement.showsCompletionBanner = YES;

        if (![[NSUserDefaults standardUserDefaults] boolForKey:identifier]) {
            //Tell analytics if you want to
        }

        NSArray *achievements = @[achievement];
        [GKAchievement reportAchievements:achievements withCompletionHandler:^(NSError *error) {
            if (error != nil) {
                NSLog(@"Error in reporting achievements: %@", error);
            } else {
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:identifier];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
        }];
    }
}

Which are both located in the TCUser file I've created, after the authentication of the local user.

The tutorial I was following gave this line of code for reporting:

[SingletonClassName reportAchievementIdentifier:@"com.companyname.gamename.achievementname" percentComplete:100.0f];

But I don't understand which singleton class to reference. Can anyone help me out and show me how exactly to report a score or achievement?

The Apple documentation didn't help either. PS This was the tutorial: http://www.raywenderlich.com/forums/viewtopic.php?f=2&t=10844

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Liam Stockhomme
  • 523
  • 3
  • 20
  • 1
    Just substitute `SingletonClassName` with the name of the class you put those methods in. Well, and provide the correct arguments. – Justin Johns May 24 '14 at 23:01
  • They are in a separate file called TCUser, which is linked to the documents it has to be linked to. At the top above the implementation the tutorial said to make it a class of myScene (the game page) so I tried replacing it with myScene but it didn't work either. What exactly is a singleton class? i've never worked with them before. @JustinJohns – Liam Stockhomme May 24 '14 at 23:06
  • The `+` denotes a class method. You invoke it with the name of the class, not an instance of the class. – Justin Johns May 25 '14 at 01:58

0 Answers0