I’m wanting to integrate Game Center into my cocos2d-x project and am wondering if there are any good tutorials out there. Anyone know of one? I don’t have any obj-c programming experience, hence the need for a tutorial with mixing obj-c with my cocos2d-x c++ code. Thanks guys!
You can search it in this forum or by google.
https://developer.apple.com/library/ios/documentation/LanguagesUtilities/Conceptual/iTunesConnectGameCenter_Guide/iTunesConnectGameCenter_Guide.pdf
http://forum.spritebuilder.com/t/im-freaking-out-with-game-center/1183/8
https://github.com/juanpdelat/ios-gamecenter-singleton/blob/master/GameCenter/GameCenterManager.m#pragma mark - GameCenter Manager Delegate
http://forum.cocos2d-iphone.org/t/reportscore-throwing-error/12538/2
https://developer.apple.com/library/ios/documentation/LanguagesUtilities/Conceptual/iTunesConnectGameCenter_Guide/Achievements/Achievements.html#//apple_ref/doc/uid/TP40013726-CH3-SW3
http://stackoverflow.com/questions/6278085/integrate-gamecenter-in-cocos2d-game
http://www.stevebarnegren.com/1/post/2014/01/game-kit-helper-for-c-cocos2d-x.html
//http://www.stevebarnegren.com/1/post/2014/01/game-kit-helper-for-c-cocos2d-x.html
- (void) leaderBoard:(NSObject )prms
{
if ( [[UIDevice currentDevice].systemVersion floatValue]>6.5)
{
NSLog(@“leaderBoard”);
NSDictionary parameters = (NSDictionary)prms;
NSLog(@“Passed params are : %@”, parameters);
// Fetching the name of the method to be called from Native to C++
// For a ease of use, i have passed the name of method from C++
NSString CPPFunctionToBeCalled = (NSString*)[parameters objectForKey:@“to_be_called”];
[self ShowLeaderboards];
// [self ShowAchievements];
[IOSNDKHelper SendMessage:CPPFunctionToBeCalled WithParameters:nil];
}
}
-(void) getDeviceId:(NSObject*)prms
{
NSLog(@“getDeviceId”);
NSDictionary parameters = (NSDictionary)prms;
NSLog(@“Passed params are : %@”, parameters);
// Fetching the name of the method to be called from Native to C++
// For a ease of use, i have passed the name of method from C++
NSString* CPPFunctionToBeCalled = (NSString*)[parameters objectForKey:@“to_be_called”];
UIDevice *device = [UIDevice currentDevice];
NSString *currentDeviceId = [[device identifierForVendor]UUIDString];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:currentDeviceId forKey:@“DEVICE_ID”];
[IOSNDKHelper SendMessage:CPPFunctionToBeCalled WithParameters:dictionary];
}
//https://developer.apple.com/library/ios/documentation/LanguagesUtilities/Conceptual/iTunesConnectGameCenter_Guide/Achievements/Achievements.html#//apple_ref/doc/uid/TP40013726-CH3-SW3
//http://stackoverflow.com/questions/19013328/unable-to-sign-in-sandbox-gamecenter-on-ios-7
//http://stackoverflow.com/questions/6278085/integrate-gamecenter-in-cocos2d-game
- (void) ShowLeaderboards
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController !=NULL)
{
leaderboardController.category = currentLeaderBoard;
leaderboardController.timeScope = GKLeaderboardTimeScopeWeek;
leaderboardController.leaderboardDelegate = self;
[self presentModalViewController: leaderboardController animated: YES];
}
}
-
(void) ShowAchievements
{
GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init];
if (achievements != nil)
{
achievements.achievementDelegate = self;
// Create an additional UIViewController to attach the GKAchievementViewController to
UIViewController *myViewController = [[UIViewController alloc] init];
g_myViewController = myViewController;
// Old method for adding sub-view (no longer works in COCOS2D-x V2: [[EAGLView sharedEGLView] addSubview:myViewController.view];
[[ [ UIApplication sharedApplication ] keyWindow ] addSubview:myViewController.view ];
[myViewController presentModalViewController:achievements animated:YES];
}
[achievements release];
} -
(void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
NSLog(@“achievementViewControllerDidFinish”);
UIDevice *ud;
// ud = [UIDevice alloc];
// id iDeviceType = [ud platformType];
NSString *platform = [self platformRawString ];
[g_myViewController dismissModalViewControllerAnimated:YES];
// if( iDeviceType!=UIDevice1GiPad && iDeviceType!=UIDevice2GiPad && iDeviceType!=UIDevice3GiPad )
{
[g_myViewController.view.superview removeFromSuperview];
[g_myViewController release];
}
[ud release];
}
- (void)leaderboardViewControllerDidFinish:(GKAchievementViewController *)viewController
{
NSLog(@“leaderboardViewControllerDidFinish”);
[g_myViewController dismissModalViewControllerAnimated:YES];
NSString *platform = [self platformRawString];
// if(![platform isEqualToString:@“iPod1,1”] && [platform isEqualToString:@“iPod2,1”]&& [platform isEqualToString:@“iPod3,1”])
{
[g_myViewController.view.superview removeFromSuperview];
[g_myViewController release];
}
//[ud release];
[self dismissModalViewControllerAnimated: YES];
// [viewController release];
}
-(void) addScoreToLeaderBoard:(NSObject )prms
{
NSLog(@“addScoreToLeaderBoard”);
NSDictionary parameters = (NSDictionary)prms;
NSLog(@“Passed params are : %@”, parameters);
// Fetching the name of the method to be called from Native to C++
// For a ease of use, i have passed the name of method from C++
NSString CPPFunctionToBeCalled = (NSString*)[parameters objectForKey:@“to_be_called”];
NSString* score=(NSString*)[parameters objectForKey:@“score”];
int64_t scoreInt64 = atoll([score UTF8String]);
currentScore=scoreInt64;
BOOL available = [[GameCenterManager sharedManager] checkGameCenterAvailability];
if (available)
{
[self.navigationController.navigationBar setValue:@“GameCenter Available” forKeyPath:@“prompt”];
[self reportScore: scoreInt64 forLeaderboardID:category];
[self completeMultipleAchievements:scoreInt64];
} else
{
[self.navigationController.navigationBar setValue:@“GameCenter Unavailable” forKeyPath:@“prompt”];
}
[IOSNDKHelper SendMessage:CPPFunctionToBeCalled WithParameters:nil];
}
- (void) reportScore: (int64_t) scoreInt64 forLeaderboardID: (NSString*) category_id
{
//http://forum.cocos2d-iphone.org/t/reportscore-throwing-error/12538/2
NSLog(@“reportScore”);
GKScore *scoreReporter =NULL;
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0) {
scoreReporter = [[GKScore alloc] initWithCategory:category_id];
} else {
scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier:category_id];
}
scoreReporter.value = scoreInt64;
scoreReporter.context = 0;
NSArray *scores = @[scoreReporter];
[GKScore reportScores :scores withCompletionHandler:^(NSError *error)
{ NSLog(@“error %@”,[error description]); } ];
}
// Adds a button on main screen
-
(void) addButton
{
// UIButton *tapButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// [tapButton setTitle:@“Tap to change text” forState:UIControlStateNormal];
// [tapButton addTarget:self action:@selector(ChangeSomethingInCocos) forControlEvents:UIControlEventTouchUpInside];
// tapButton.frame = CGRectMake(0, 150, 150, 80);
//
// [self.view addSubview:tapButton];
} -
(void) ChangeSomethingInCocos
{
[IOSNDKHelper SendMessage:@“ChangeLabelSelector” WithParameters:nil];
}
//------------------------------------------------------------------------------------------------------------//
//------- GameKit Delegate -----------------------------------------------------------------------------------//
//------------------------------------------------------------------------------------------------------------//
///////////////////////////////////////////
-
(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willBeginBannerViewActionNotification:) name:BannerViewActionWillBegin object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishBannerViewActionNotification:) name:BannerViewActionDidFinish object:nil];
[IOSNDKHelper SetNDKReciever:self];
category=[[NSString alloc] initWithString:@“hgjkhkj”]; //get category id of leaderboard from game center.[[GameCenterManager sharedManager] setDelegate:self]; currentScore = 0; if ([[GameCenterManager sharedManager] checkGameCenterAvailability]) { gameCenterManager = [[[GameCenterManager alloc] init] autorelease]; // gameCenterManager.autorelease; [gameCenterManager setDelegate:self]; [gameCenterManager authenticateLocalUser]; } else { // The current device does not support Game Center. NSLog(@"Current device does not support game center"); }
}
return self;
}