★ TouchArcade needs your help. Click here to support us on Patreon.

Trouble bringing up leaderboards with my code

07-04-2012, 07:05 PM
#1
Joined: Dec 2011
Posts: 12
Trouble bringing up leaderboards with my code

I'm having trouble getting the leaderboards to show up in my game, short of my initial login, i have no idea if gamecenter is cooperating with my code. I prompt the leaderboards using a touch detect on some text, ala...

Quote:
leaderboard = [CCMenuItemFont itemFromString:@"Online Leaderboards" target:self selector:@selector(leaderboardCmd: )];
and...

Quote:
- (void) leaderboardCmd: (id) sender {

[self showLeaderboard];

}
Which then triggers...

Quote:
- (void) showLeaderboard {

GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];

if (leaderboardController != nil)
{
leaderboardController.category = @"highscores";
leaderboardController.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardController.leaderboardDelegate = self;
[self presentModalViewController: leaderboardController animated: YES];
}

}
The app simply crashes. It seems as though the app crashes when executing "presentModalViewController", which looks like a function of UIKit? The apple provided GKTapper code doesn't shed any light on how to properly implement this into a CCLayer.

Can anyone help? Or is there an existing example other than GKTapper?
07-04-2012, 07:36 PM
#2
Joined: Jul 2012
Location: Argentina
Posts: 17
I think you are missing the leaderboardViewControllerDidFinish: method. One implementation (that I haven't checked) could be the following, although I had found one-liners as well:


-(void)leaderboardViewControllerDidFinishGKLeader boardViewController*)viewController{
CGRect frame = viewController.view.frame;
[UIView beginAnimations:@"curldoup" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationDidStopSelector:
@selector(animationDidStop:finished:context];
frame.origin.y = 480;
viewController.view.frame = frame;
[UIView commitAnimations];
}

(firmarķa si encontrara la cruz)
Nico

07-05-2012, 09:26 AM
#3
My guess would be that you are just missing the required delegate template in the .h header:

Code:
@interface MyVC : UIViewController <GKLeaderboardViewControllerDelegate> {

}
This delegate is required in any class that you assign to .leaderboardDelegate.

Apps: Lead Wars (TA Thread) | Super Grav (TA Thread) | 3D Spinner (TA Thread)
DemonStudios Ltd | Indie Game Dev
www.demonstudios.com
Follow: Facebook | Twitter
07-05-2012, 10:06 AM
#4
Joined: Nov 2010
Location: Portland, OR
Posts: 87
You can't pop a UIViewController directly from a CCLayer, you need to do it from within the context of another UIViewController.

First, expose your RootViewController as a property from your AppDelegate:
@property (nonatomic, retain) RootViewController *viewController;

Then, import your AppDelegate in your CCLayer.h file.

Next:

GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
leaderboardController.leaderboardDelegate = self;

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
RootViewController *vCont = appDelegate.viewController;
[vCont presentModalViewController:leaderboardController animated:YES];

[leaderboardController release];
07-05-2012, 09:26 PM
#5
Joined: Dec 2011
Posts: 12
If i understand correctly, the leaderboard must be popped from a UIViewController?
07-05-2012, 09:45 PM
#6
Joined: Nov 2010
Location: Portland, OR
Posts: 87
Correct. It's just like popping a modal view controller in UIKit.

There are a number of people who have written sample code for this and suggest using a temporary view controller, but I prefer to do this against my app's root view controller as that seems to yield better results when dealing with orientation changes.
07-08-2012, 03:23 AM
#7
Joined: Jan 2012
Location: Warsaw, Poland
Posts: 34
I had very similar issue, but I found a simple solution, like this:

HighscoreScene.h:
Code:
@interface HighscoreScene : BaseScene <GKLeaderboardViewControllerDelegate>
{
(...)
}

(...)
- (void) showLeaderboard;
- (void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)leaderboardVC;
(...)
(where BaseScene inherits from NSObject)

HighscoreScene.m:
Code:
- (void) showLeaderboard
{
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
	
    GKLeaderboardViewController *leaderboardVC = [[GKLeaderboardViewController alloc] init];   
    if (leaderboardVC != nil)
    {
	leaderboardVC.leaderboardDelegate = self;
		
	UIViewController *vc = [[UIViewController alloc] init];
	[window addSubview: vc.view];
        [vc presentModalViewController: leaderboardVC animated: YES];
    }
}

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)leaderboardVC
{
    [leaderboardVC dismissModalViewControllerAnimated:YES];
    [leaderboardVC.view.superview removeFromSuperview];
    [leaderboardVC release];
}

-=Agnes=-