Tutorial: Use GameCenter with cococs2d-x

A few weeks earlier I have published a tutorial how to use Google Play Games Services with cocos2d-x, I have uploaded an implementation of that code to github: https://github.com/dwd31415/GameSharing , that code is called GameSharing(https://adriandawid.wordpress.com/game-sharing/) and I also added an implementation of this tutorial to it. If you want to use GameSharing you may follow this tutorial and instead of creating your own wrappers add the GameSharing code file to your project.


In this tutorial I will explain how to use Apples Game Center with your Cocos2d-X game. You can do that actually very easily, for Objective-C++ is C++ compatible.

1.Add GameKit to your project

In order to use GameCenter at all you must add the GameKit framework to your project, you can do that very easily over Xcode:

 

Bildschirmfoto 2014-09-08 um 22.39.09

 

2. Preparing your app in iTunes Connect

To use GameCenter you must be an iOS Developer. To set it up, please add your app to your iOS connect account and add all GameCenter Leaderboards and Achievements you need in there.

3.Establish a connection between Objective-C++ and C++ Code

This step might sound difficult, but it is very easy because an Objective-C++ compiler can compile any C++ code too. Therefore we can create a c++ header, use it in our cocos2d-x game code and implement the class in Objective-C++. And thats exactly the way I did it in GameSharing, so all you have to do is create a class with functions for all GameCenter features you want to use and then add  an Objective-C++ file(.mm) to your iOS Project, that looks like this:

#import”yourheader.h”

 

void YourClass::YourFunction()

{

//Your Objective-C++ code

}

4. Preparing our AppController and RootViewController for GameCenter

To use GameCenter in your game you have to change the class definition of RootViewController to:

#import <UIKit/UIKit.h>

#import “AppController.h”

#import <GameKit/GameKit.h>

@interface RootViewController : UIViewController <GKAchievementViewControllerDelegate,

GKLeaderboardViewControllerDelegate>  {

}

– (BOOL) prefersStatusBarHidden;

@end

 

And the class definition of AppController to:

#import <UIKit/UIKit.h>

#import <GameKit/GameKit.h>

#import “RootViewController.h”

@interface AppController : NSObject <UIApplicationDelegate,GKLeaderboardViewControllerDelegate,

GKAchievementViewControllerDelegate> {

    UIWindow *window;

}

@property(nonatomic, readonly) RootViewController* viewController;

@end

Then add this functions to both:

– (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController

{

    AppController* delegate = (AppController*) [UIApplication sharedApplication].delegate;

    [delegate.viewController dismissModalViewControllerAnimated:YES];

    

}

-(void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController

{

    AppController* delegate = (AppController*) [UIApplication sharedApplication].delegate;

    [delegate.viewController dismissModalViewControllerAnimated:YES];

}

6. Implement GameCenter features

The last thing to do is implement your GameCenter features in Objective-C++, add wrapper functions to your .h in the Classes folder and then implement them in a Objective-C++ file. How to use the GameKit API is documented here: bit.ly/iOSGameCenterGuide . If you don’t want to start your wrapper from scratch, you can use GameSharing and add the .cpp and .h files to your Classes, and the .mm file to your iOS Project folder. If you do that please take a look at the wiki: https://github.com/dwd31415/GameSharing/wiki/Basic-configuration.

7. A little screenshot:

Here is a little screenshot how your project might look like:

Screenshot_7

6 thoughts on “Tutorial: Use GameCenter with cococs2d-x

  1. John Carlyle says:

    Why does the ‘How to use GameSharing’ instructions here https://github.com/dwd31415/GameSharing not mention changes to RootViewController, nor the added functions to RootViewController and AppController ?

    Why is there no mention of calling initGameSharing() in your guides?

    This is excellent work, but severly let down by poor documentation.

    Like

Leave a comment