-
JamesirYaoLikes 0Problem Description
I used to watch this tutorial (http://bit.ly/1Fle4WR), but it just provide a method of showing Apple’s default leaderboard.
I’m bad in Objective-C and I just wonder how to fetch the leaderboard data?
Thanks u guys!.
-
Sonar Systems adminLikes 0
What dad are you trying to retrieve?
-
JamesirYaoLikes 0
Thanx for replying.
Just the scores and users’ name
I just made a trial and added a function in your CocosHelperCPP.
And now I’m finding the method using lambda callback function between CPP and OBJ-C.
If I fail I’ll reply for help again LOL.
Following is the code.
-( void ) gameCenterGetTopXScore:(int)number andLeaderboard: (NSString *)leaderboardID;
- ( void ) gameCenterGetTopXScore:(int)number andLeaderboard:(NSString *)leaderboardID
{
GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
if (leaderboardRequest != nil)
{
leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
leaderboardRequest.timeScope = GKLeaderboardTimeScopeWeek;
leaderboardRequest.range = NSMakeRange(1,number);
leaderboardRequest.category = leaderboardID;
[leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
if (error != nil){
// handle the error.
NSLog(@"Failed to download");
}
if (scores != nil){
// process the score information.
NSLog(@"Succeeded to download");
NSArray *tempScore = [NSArray arrayWithArray:leaderboardRequest.scores];
int * value = new int[number];
std::string * nickName = new std::string[number];
for (GKScore *obj in tempScore) {
int rank = (int)obj.rank-1;
value[rank] = (int)obj.value;
nickName[rank] = [obj.player.alias UTF8String];
NSLog(@" rank : %d",rank);
NSLog(@" value : %d",value[rank]);
NSLog(@" name : %s",nickName[rank].c_str());
NSLog(@"**************************************");
}
}
}];
}
}
-
Sonar Systems adminLikes 0
What happens when you call that method?
-
JamesirYaoLikes 0
Before the data download, there is a DOWNLOADING animation in the layer.
As the download complete, a custom leaderboard will appear.
So the callback is to show the leaderboard based on the downloaded data.
-
Sonar Systems adminLikes 0
Do you get the desired data back?
-
JamesirYaoLikes 0
Now I have solved my problem and just renamed the IOSHelper.m to IOSHelper.mm.
The final code:
- ( void ) gameCenterGetTopXScore:(int)number andLeaderboard:(NSString *)leaderboardID andCallback:(std::function<void(bool,int,int*,std::string*)> *) function
{
GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
if (leaderboardRequest != nil)
{
leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
leaderboardRequest.timeScope = GKLeaderboardTimeScopeWeek;
leaderboardRequest.range = NSMakeRange(1,number);
leaderboardRequest.category = leaderboardID;
[leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
if (error != nil){
// handle the error.
NSLog(@"Failed to download");
if (function) (*function) (false, 0, nullptr, nullptr);
}
if (scores != nil){
// process the score information.
NSLog(@"Succeeded to download");
NSArray *tempScore = [NSArray arrayWithArray:leaderboardRequest.scores];
int * value = new int[number];
std::string * nickName = new std::string[number];
int n = 0;
for (GKScore *obj in tempScore) {
n++;
int rank = (int)obj.rank-1;
value[rank] = (int)obj.value;
nickName[rank] = [obj.player.alias UTF8String];
}
if (function) (*function) (true, n, value, nickName);
}
}];
}
}
-
Sonar Systems adminLikes 0
Good to hear you solved it.
Login to reply