Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
2  
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? –  lemikegao Dec 10 '12 at 3:27

2 Answers 2

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];
share|improve this answer
    
I am using cocos2d 1.0.1 so what should I do? –  Coder404 Dec 12 '12 at 0:46
    
Updated the answer with info for previous versions –  So Much Drama Studios Dec 12 '12 at 15:00

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.