注意:在越狱手机上测试GameCenter,可能会出现 This game is not recongnized by Game Center. 此时请用没越狱手机测试
http://hi.baidu.com/qq5292485/blog/item/61c4e60e069c900a6159f374.html
1、cocos2d-x是基于c++代码,而apple提供的api是基于object-c,此时你需要先了解c++中如何调用object-c进行混合编程的,这个可以参考http://www.himigame.com/iphone-cocos2dx/743.html这篇文章,写的很详细,接着参考http://www.himigame.com/iphone-cocos2d/535.html接入gamecenter,但补充一点,就是在游戏中,当需要通过按钮直接显示出排行榜的时候,又该如何实现呢?
查了下苹果的官方文档找到了 哈哈,代码如下:(*前提该类必须要实现GKLeaderboardViewController<GKLeaderboardViewControllerDelegate>)
- (void) showLeaderboard |
{ |
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init]; |
if (leaderboardController != nil) |
{ |
leaderboardController.leaderboardDelegate = self; |
[self presentModalViewController: leaderboardController animated: YES]; |
} |
} |
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController |
{ |
[self dismissModalViewControllerAnimated:YES]; |
} |
- (void) showLeaderboard
{
tempVC=[[UIViewControlleralloc] init];
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewControlleralloc] init];
if (leaderboardController != nil)
{
UIApplication* clientApp = [UIApplicationsharedApplication];
UIWindow* topWindow = [clientApp keyWindow];
if (!topWindow)
{
topWindow = [[clientApp windows] objectAtIndex:0];
}
[topWindow addSubview:tempVC.view];
leaderboardController.leaderboardDelegate = self;
[tempVCpresentModalViewController: leaderboardController animated: YES];
}
}
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[tempVCdismissModalViewControllerAnimated:YES];//在iphone上需要设置为NO,才可以继续响应事件(笔者还没有找到其他解决办法)
[tempVC.viewremoveFromSuperview]; //为了使排行榜关闭的时候,能够继续响应事件
}
下面给出官方对于显示成就的代码(将其转换到cocos2d-x跟上面是一样原理了)
- (void) showAchievements |
{ |
GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init]; |
if (achievements != nil) |
{ |
achievements.achievementDelegate = self; |
[self presentModalViewController: achievements animated: YES]; |
} |
[achievements release]; |
} |
- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController |
{ |
[self dismissModalViewControllerAnimated:YES]; |
} |