Trouble bringing up leaderboards with my code

Discussion in 'Public Game Developers Forum' started by Stafaa, Jul 5, 2012.

  1. Stafaa

    Stafaa Member

    Dec 17, 2011
    12
    0
    0
    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...

    and...

    Which then triggers...

    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?
     
  2. Nicor

    Nicor Member

    Jul 2, 2012
    17
    0
    0
    Argentina
    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)leaderboardViewControllerDidFinish:(GKLeaderboardViewController*)viewController{
    CGRect frame = viewController.view.frame;
    [UIView beginAnimations:mad:"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];
    }
     
  3. DemonJim

    DemonJim Well-Known Member

    Nov 19, 2010
    409
    0
    0
    App Developer
    UK
    My guess would be that you are just missing the required delegate template in the .h header:

    Code:
    @interface MyVC : UIViewController [B]<GKLeaderboardViewControllerDelegate>[/B] {
    
    }
    This delegate is required in any class that you assign to .leaderboardDelegate.
     
  4. u2elan

    u2elan Well-Known Member

    Nov 8, 2010
    87
    0
    0
    iOS Developer
    Portland, OR
    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];
     
  5. Stafaa

    Stafaa Member

    Dec 17, 2011
    12
    0
    0
    If i understand correctly, the leaderboard must be popped from a UIViewController?
     
  6. u2elan

    u2elan Well-Known Member

    Nov 8, 2010
    87
    0
    0
    iOS Developer
    Portland, OR
    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.
     
  7. AgnesDev

    AgnesDev Active Member

    I had very similar issue, but I found a simple solution, like this:

    HighscoreScene.h:
    Code:
    @interface HighscoreScene : [U]BaseScene[/U] [B]<GKLeaderboardViewControllerDelegate>[/B]
    {
    [I](...)[/I]
    }
    
    [I](...)[/I]
    - (void) [B]showLeaderboard[/B];
    - (void) [B]leaderboardViewControllerDidFinish[/B]:(GKLeaderboardViewController *)leaderboardVC;
    [I](...)[/I]
    
    
    (where BaseScene inherits from NSObject)

    HighscoreScene.m:
    Code:
    - (void) [B]showLeaderboard[/B]
    {
        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)[B]leaderboardViewControllerDidFinish[/B]:(GKLeaderboardViewController *)leaderboardVC
    {
        [leaderboardVC dismissModalViewControllerAnimated:YES];
        [leaderboardVC.view.superview removeFromSuperview];
        [leaderboardVC release];
    }
    

    -=Agnes=-
     

Share This Page