Monday, May 14, 2012

Cocos2d/Gamecenter: How to implement correctly GameCenter into Cocos2d

Hi,

How's it going there?
Here's a tutorial that i saw loads of people having the same problem and after some research  I figured it out.

I don't know if it's the ideal way, but as it worked I can suit you.

Cocos2D: 1.0.1

What was the problem that it wouldn't work?
R: For some reason the RootViewController wasn't referenced and so it gave me a 12h delay on the project.

Step 1:

Go to you AppDelegate.h


#import <UIKit/UIKit.h>

@class RootViewController;

@interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *viewController;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) RootViewController *viewController;

@end



Step 2:

Go to your AppDelegate.m


#import "cocos2d.h"



#import "AppDelegate.h"

#import "RootViewController.h"

@implementation AppDelegate

@synthesize window;
@synthesize viewController;

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.rootViewController = viewController;
//blablablá
// Init the View Controller
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
}

Step 3:

Import GameKit.framework

Step 4:

Implement some code.
I'm going to put only some basic stuff and of course the most important the leaderboard.



-(void)localUserAuthentication{

    

    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    [localPlayer authenticateWithCompletionHandler:^(NSError *error) {
        if (!error){
            isLocalUserAuthenticated = YES;
            //NSLog(@"AUTHETICATION SUCCEDED");
        } else {
            isLocalUserAuthenticated = NO;
            //NSLog(@"AUTHETICATION FAILED");
        }
    }];
}





-(void)showLeaderboard{
    
    
    GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
    if (leaderboardController != NULL) {
        leaderboardController.category = @"com.yourcompany.gamename.leaderboard.whatleaderboard"
        leaderboardController.timeScope =GKLeaderboardTimeScopeAllTime;
        leaderboardController.leaderboardDelegate = self
        AppDelegate *delegate = [UIApplication sharedApplication].delegate
        [delegate.viewController presentModalViewController:leaderboardController animated:YES];
    }
    
    leaderboardController.toolbar.autoresizesSubviews = YES;
    leaderboardController.view.bounds = CGRectMake(0, 0, 480, 320);
    leaderboardController.view.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90));
}



-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController{
    CCMenu* gamemenu = (CCMenu*)[self getChildByTag:kGameMenu];
    gamemenu.isTouchEnabled = YES;
    [self show];
    
    AppDelegate *delegate = [UIApplication sharedApplication].delegate
    [delegate.viewController dismissModalViewControllerAnimated:YES];
}



Well, so that's that.
If you guys need any help, just get in contact.
Hasta luego.

Tweet