Search code examples
ioscocoacocos2d-iphonegame-centercocos2d-x

Open Game Center UI with Cocos2d-x


I have set up so that my app can connect to the Game Center. I followed this guide and had a C++ class wrapper to call them in my other classes. I'm using iPad (iOS 5.1), iPod Touch 4th Gen (iOS 5.1), and iPhone 4s and 3GS to test. It works fine on iPad devices but for some unknown reason, it does not on iPhone and iPod. It properly connects to the Sandbox but the UI is not shown, or rather, the UI is somewhere off-screen.

What happens on the iPhone is:

  1. If I'm not logged in and I access the Game Center, a login window appears. When I log in, nothing happens. However, the console says a view is added.
  2. If I'm logged in and I access the Game Center, nothing happens. However, the console says a view is added.
  3. If I access the Game Center before I get authenticated, the Game Center UI appears. However...
    • The Game Center is in Landscape Mode (no problem there, since my app is in landscape) but the top bar (the bar with the DONE button) is placed as if the orientation is portrait.
    • The whole UI is not shown, just ~30% of the screen.

This is how I launch the Game Center UI:

- (void)showLeaderboardForCategory:(NSString *)category
{
    // Only execute if OS supports Game Center & player is logged in
    if ( hasGameCenter )
    {
        // Create leaderboard view w/ default Game Center style
        GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];

        // If view controller was successfully created...
        if (leaderboardController != nil)
        {
            // Leaderboard config
            leaderboardController.leaderboardDelegate = self;   // The leaderboard view controller will send messages to this object
            leaderboardController.category = category;  // Set category here
            leaderboardController.timeScope = GKLeaderboardTimeScopeToday;  // GKLeaderboardTimeScopeToday, GKLeaderboardTimeScopeWeek, GKLeaderboardTimeScopeAllTime

            // Create an additional UIViewController to attach the GKLeaderboardViewController to
            myViewController = [[UIViewController alloc] initWithNibName:nil bundle:nil ];

            // Add the temporary UIViewController to the main OpenGL view
            // NOTE: This is the part that I think is the suspect. I am not sure if iPhones and iPods support EAGLView.
            [ [ EAGLView sharedEGLView ] addSubview:myViewController.view ];

            // Tell UIViewController to present the leaderboard
            [ myViewController presentModalViewController:leaderboardController animated:NO ];
            NSLog( @"Leaderboard opened." );
        }
    }
}

Any help is appreciated and thanks in advance.

EDIT: Using OpenFeint is not an option.


Solution

  • Try this.Do not use 'EAGLView'

    - (void) showLeaderboard
    {
        if (!gameCenterAvailable) return;
    
        GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
        if (leaderboardController != nil) {
            leaderboardController.leaderboardDelegate = self;
    
            UIWindow *window = [[UIApplication sharedApplication] keyWindow];
            currentModalViewController = [[UIViewController alloc] init];
            [window addSubview:currentModalViewController.view];
            [currentModalViewController presentModalViewController:leaderboardController animated:YES];
        }
    
    }