10

Is there any one who knows how to integrate game center in Cocos2d. Please tell me steps so i can integrate that in my Game.

Alok
  • 24,880
  • 6
  • 40
  • 67
Vivek2012
  • 772
  • 1
  • 13
  • 25

3 Answers3

30

UPDATE:

I created my own Helper Class that works with all kind of Apps (also Cocos2D 1 & 2+) https://github.com/alexblunck/ABGameKitHelper


Hi I suggest you use GKHelper Class from Steffen Itterheim! I uploaded the GKHelper.h / GKHelper.m for you: http://www.cl.ly/7ReW

Then follow these instructions:

//0.0 Add GameKit Framework to Project (Ask If you don't know how to do this ;) )

//0. Change "[window addSubview: viewController.view];" in the AppDelegate.m to: //Do this if you're using any release of cocos2D after 0.99.5:

window.rootViewController = viewController;

//1. Add Gamekithelper.h / .m to project

//2. Include following delegate in given header:

<GameKitHelperProtocol>

//3. Add Delegate Methods to .m

//4. Add GameKitHelper to "Scene":

GameKitHelper *gkHelper = [GameKitHelper sharedGameKitHelper];
gkHelper.delegate = self;
[gkHelper authenticateLocalPlayer];

//Adding score to leaderboard:

GameKitHelper *gkHelper = [GameKitHelper sharedGameKitHelper];
[gkHelper submitScore:scoreValue category:@"LeaderboardID"];

//Adding achievement completion:

GameKitHelper *gkHelper = [GameKitHelper sharedGameKitHelper];
[gkHelper reportAchievementWithID:@"AchievementID" percentComplete:100];

These are the delegate Methods that need to be added mentioned in Step #3:

#pragma mark GameKitHelper delegate methods
-(void) onLocalPlayerAuthenticationChanged
{
    GKLocalPlayer* localPlayer = [GKLocalPlayer localPlayer];
    CCLOG(@"LocalPlayer isAuthenticated changed to: %@", localPlayer.authenticated ? @"YES" : @"NO");

    if (localPlayer.authenticated)
    {
        GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper];
        [gkHelper getLocalPlayerFriends];
        //[gkHelper resetAchievements];
    }   
}
-(void) onFriendListReceived:(NSArray*)friends
{
    CCLOG(@"onFriendListReceived: %@", [friends description]);
    GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper];
    [gkHelper getPlayerInfo:friends];
}
-(void) onPlayerInfoReceived:(NSArray*)players
{
    CCLOG(@"onPlayerInfoReceived: %@", [players description]);


}
-(void) onScoresSubmitted:(bool)success
{
    CCLOG(@"onScoresSubmitted: %@", success ? @"YES" : @"NO");
}
-(void) onScoresReceived:(NSArray*)scores
{
    CCLOG(@"onScoresReceived: %@", [scores description]);
    GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper];
    [gkHelper showAchievements];
}
-(void) onAchievementReported:(GKAchievement*)achievement
{
    CCLOG(@"onAchievementReported: %@", achievement);
}
-(void) onAchievementsLoaded:(NSDictionary*)achievements
{
    CCLOG(@"onLocalPlayerAchievementsLoaded: %@", [achievements description]);
}
-(void) onResetAchievements:(bool)success
{
    CCLOG(@"onResetAchievements: %@", success ? @"YES" : @"NO");
}
-(void) onLeaderboardViewDismissed
{
    CCLOG(@"onLeaderboardViewDismissed");

    GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper];
    [gkHelper retrieveTopTenAllTimeGlobalScores];
}
-(void) onAchievementsViewDismissed
{
    CCLOG(@"onAchievementsViewDismissed");
}
-(void) onReceivedMatchmakingActivity:(NSInteger)activity
{
    CCLOG(@"receivedMatchmakingActivity: %i", activity);
}
-(void) onMatchFound:(GKMatch*)match
{
    CCLOG(@"onMatchFound: %@", match);
}
-(void) onPlayersAddedToMatch:(bool)success
{
    CCLOG(@"onPlayersAddedToMatch: %@", success ? @"YES" : @"NO");
}
-(void) onMatchmakingViewDismissed
{
    CCLOG(@"onMatchmakingViewDismissed");
}
-(void) onMatchmakingViewError
{
    CCLOG(@"onMatchmakingViewError");
}
-(void) onPlayerConnected:(NSString*)playerID
{
    CCLOG(@"onPlayerConnected: %@", playerID);
}
-(void) onPlayerDisconnected:(NSString*)playerID
{
    CCLOG(@"onPlayerDisconnected: %@", playerID);
}
-(void) onStartMatch
{
    CCLOG(@"onStartMatch");
}
-(void) onReceivedData:(NSData*)data fromPlayer:(NSString*)playerID
{
    CCLOG(@"onReceivedData: %@ fromPlayer: %@", data, playerID);
}
Alexander Blunck
  • 1,224
  • 13
  • 17
  • 1
    Just be aware that if you ever want to use GameKitHelper for a multi-player app, the class does not adequately cater for the scenario where the device has multi-player games restricted within the Restrictions app on the device. – PKCLsoft Jul 02 '12 at 13:53
  • Your GameKitHelper doesn't work properly. I can't even submit a score immediatelly after authentication, because the event of chenging authentication state is called on each [GKLocalPlayer localPlayer] – Gargo Sep 23 '12 at 20:02
  • 1
    Hi, like I mentioned in the answer, this isn't my helper class and is since been outdated. I did create one of my own though: http://github.com/ablfx/ABGameKitHelper – Alexander Blunck Sep 23 '12 at 23:11
  • How can i disable the Challenge Friends Button in Games Center? – Sudhakar Oct 16 '12 at 06:58
  • @Sudhakar Check the answer on your original question ;) – Alexander Blunck Oct 18 '12 at 09:21
  • @AlexanderBlunck In step #2 where do I put the delegate? In my App Delegate? In my first secene? – Coder404 Dec 09 '12 at 18:43
  • @Coder404 If you use my Helper Class (Link on top) you won't to need to add that at all....otherwise in each scene (CCLayer Class) you plan to use GameCenter. I'd check out my helper class, since it abstracts all that into a separate class. – Alexander Blunck Dec 10 '12 at 08:18
  • Do you give a tutorial on how to implement it? – Coder404 Dec 10 '12 at 12:17
  • @Coder404 I added a Tutorial / Instructions for you ... see the github page: https://github.com/ablfx/ABGameKitHelper ;) – Alexander Blunck Dec 11 '12 at 12:44
  • @AlexanderBlunck Awesome!!!!! I will definitely implement this into my project! Thanks!!!!! – Coder404 Dec 12 '12 at 00:05
  • @AlexanderBlunck Um what license is it under? and what does it mean? – Coder404 Dec 12 '12 at 00:36
  • Like ot says on the github page...MIT Licens aka do what you want with it – Alexander Blunck Dec 12 '12 at 06:09
  • @AlexanderBlunck so the license means that I could use your code and I wouldn't have to do anything? – Coder404 Dec 14 '12 at 03:55
  • @AlexanderBlunck so why have it? – Coder404 Dec 14 '12 at 12:03
  • @Coder404 ...so people like you don't get confused – Alexander Blunck Dec 14 '12 at 15:20
  • it just shows up a blank white screen, and the log says ERROR can't authenticate – Ferenc Dajka May 12 '13 at 16:41
  • @FerencDajka You have to integrate the class into a project / app that has a valid App Id with Game Center enabled – Alexander Blunck May 12 '13 at 20:34
  • I tried to run your example project, I replaced the ID, maybe I didn't added the proper string. Gonna check it. Thanks – Ferenc Dajka May 12 '13 at 23:06
  • @AlexanderBlunck many thanks for your help, and for the code as well, finally I made it work. I'll spread your glorius name over the galaxy! – Ferenc Dajka May 13 '13 at 20:38
  • @AlexanderBlunck hello again! I added your code to a newer version of cocos2d-x (yep I'm using x) and now I get a lot of Mach O linker error when I add your files to my project, do you know anything about it? – Ferenc Dajka May 14 '13 at 15:45
  • ok false positive, I haven't added gamekit I have 2 targets and I only added to one of them – Ferenc Dajka May 14 '13 at 16:03
  • I can't login with your helper in Cocos2d v3. Please help! ABGameKitHelper: ERROR -> Player didn't authenticate – LE SANG May 29 '14 at 09:53
3

You can go through with GamKit framework . Game center is very powerful for managing your online game and game score as well . with game center there are two types of game you can create

1: Real time matches (Real time Car racing) 2: Turn Base Matches (Online card game )

I am sharing with you link of RaywenderLich :

Real time match : http://www.raywenderlich.com/3276/game-center-tutorial-for-ios-how-to-make-a-simple-multiplayer-game-part-12

Turn Based Match http://www.raywenderlich.com/5480/beginning-turn-based-gaming-with-ios-5-part-1

Alok
  • 24,880
  • 6
  • 40
  • 67
2

Although Alexander Blunck's answer is reasonable, for earlier versions of iOS (such as 3.2) the following line will fault, which is not what you want.

window.rootViewController = viewController;

If you are going to use Steffen's code (ugh) then you might want to add a method to set the ui view controller directly rather than having it assume it can be grabbed via UIApplication.

Tom Guinther
  • 596
  • 5
  • 4