4
\$\begingroup\$

I have the following code:

-(void)showLeaderboard {

GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL)
{
    leaderboardController.timeScope = GKLeaderboardTimeScopeAllTime;
    leaderboardController.leaderboardDelegate = self;
    [self presentModalViewController: leaderboardController animated: YES];
}

[leaderboardController release];

}

Which I am trying to use to make my leader-board pop up. The issue is cocos2d will not allow me to use the line:

        [self presentModalViewController: leaderboardController animated: YES];

How do I fix this?

Thanks

PS I am using cocos2d

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You'll need to be more descriptive about your problem for us to help: - In what class are you implementing the method showLeaderboard? - What errors do you get when you run your code? \$\endgroup\$
    – lemikegao
    Dec 10, 2012 at 3:27

2 Answers 2

5
\$\begingroup\$

You can only call [self presentModalViewController...] from a UIViewController (or one of its subclasses). In Cocos2d 2.0, the CCDirector is a subclass of UIViewController, so you can do something like:

[[CCDirector sharedDirector] presentModalViewController...]

(In previous versions of Cocos2d, I think you need to latch back to the RootViewController, but I could be wrong)

BTW, presentModalViewController is now deprecated; you may want to look at the replacement method "presentViewController..." instead.

For earlier versions of Cocos2d, you can either latch onto the OpenGL view:

[CCDirector sharedDirector] openGLView]...

or hook back into the main application delegate:

AppDelegate *app = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[app methodThatDoesWhatIWantItToDo];
\$\endgroup\$
2
  • \$\begingroup\$ I am using cocos2d 1.0.1 so what should I do? \$\endgroup\$
    – Coder404
    Dec 12, 2012 at 0:46
  • \$\begingroup\$ Updated the answer with info for previous versions \$\endgroup\$ Dec 12, 2012 at 15:00
2
\$\begingroup\$

You have to implement GKAchievementViewControllerDelegate protocol in your layer or scene.

Declare your layer like this:

@interface MyLayer : CCLayer <GKAchievementViewControllerDelegate> 

This will show the game center:

- (void)showAchievements{

     NSLog(@"Show achievements!");
     AppDelegate *delegate = [UIApplication sharedApplication].delegate;

     GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init];
     if (achievements != NULL) {
        achievements.achievementDelegate = self;
        [delegate.viewController presentModalViewController: achievements animated: YES];
    }
 }

And to get back the control when user has finished with GameCenter:

- (void)achievementViewControllerDidFinish: (GKAchievementViewController *)viewController{

     AppDelegate *delegate = [UIApplication sharedApplication].delegate;
     [delegate.viewController dismissModalViewControllerAnimated: YES];
     [viewController release];
}

This code works on cocos2d 1.0.1 in iOS 4,5 & 6.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .