Cocos2d-X 添加 Game Center 的方法

本教程基于cocos2d-x 13版本编写 主要内容是告诉大家在c++中如何添加 game center ,找了很多网络上面的代码基本上都是缺胳膊少腿的,代码不完整,对于初学者来说是痛苦的。

本代码仅提供如何登陆GameCenter 显示和关闭Leaderboard(排行榜)。其他实现请参考子龙山人的博客,顺便贴下地址

http://www.cnblogs.com/zilongshanren/archive/2011/06/24/2088383.html 他讲解了很多东西。如何激活GameCenter等值得学习的文章。写的很细致哦废话不说上贴上代码咯

我这里主要如何使用UIViewController 来实现GameCenter的呈现

  1. //
  2. // GameKitHelper.h
  3. // toDefendTheEarth
  4. //
  5. // Created by jingjing on 12-6-7.
  6. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import <Foundation/Foundation.h>
  10. #import <GameKit/GameKit.h>
  11. @interface GameKitHelper : NSObject <GKLeaderboardViewControllerDelegate, GKAchievementViewControllerDelegate, GKMatchmakerViewControllerDelegate, GKMatchDelegate>{
  12. BOOL gameCenterAvailable;
  13. BOOL userAuthenticated;
  14. }
  15. @property (assign, readonly) BOOL gameCenterAvailable;
  16. + (GameKitHelper *)sharedGameKitHelper;
  17. - (void) authenticateLocalUser;
  18. - (void) showLeaderboard;
  19. - (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController;
  20. @end
复制代码

这是实现

  1. //
  2. // GameKitHelper.m
  3. // toDefendTheEarth
  4. //
  5. // Created by jingjing on 12-6-7.
  6. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "GameKitHelper.h"
  9. @implementation GameKitHelper
  10. @synthesize gameCenterAvailable;
  11. //静态初始化 对外接口
  12. static GameKitHelper *sharedHelper = nil;
  13. static UIViewController* currentModalViewController = nil;
  14. + (GameKitHelper *) sharedGameKitHelper {
  15. if (!sharedHelper) {
  16. sharedHelper = [[GameKitHelper alloc] init];
  17. }
  18. return sharedHelper;
  19. }
  20. //用于验证
  21. - (BOOL)isGameCenterAvailable {
  22. // check for presence of GKLocalPlayer API
  23. Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
  24. // check if the device is running iOS 4.1 or later
  25. NSString *reqSysVer =@"4.1";
  26. NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
  27. BOOL osVersionSupported = ([currSysVer compare:reqSysVer
  28. options:NSNumericSearch] != NSOrderedAscending);
  29. return (gcClass && osVersionSupported);
  30. }
  31. - (id)init {
  32. if ((self = [super init])) {
  33. gameCenterAvailable = [self isGameCenterAvailable];
  34. if (gameCenterAvailable) {
  35. NSNotificationCenter *nc =
  36. [NSNotificationCenter defaultCenter];
  37. [nc addObserver:self
  38. selector:@selector(authenticationChanged)
  39. name:GKPlayerAuthenticationDidChangeNotificationName
  40. object:nil];
  41. }
  42. }
  43. return self;
  44. }
  45. //后台回调登陆验证
  46. - (void)authenticationChanged {
  47. if ([GKLocalPlayer localPlayer].isAuthenticated &&!userAuthenticated) {
  48. NSLog(@"Authentication changed: player authenticated.");
  49. userAuthenticated = TRUE;
  50. } else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) {
  51. NSLog(@"Authentication changed: player not authenticated");
  52. userAuthenticated = FALSE;
  53. }
  54. }
  55. - (void)authenticateLocalUser {
  56. if (!gameCenterAvailable) return;
  57. NSLog(@"Authenticating local user...");
  58. if ([GKLocalPlayer localPlayer].authenticated == NO) {
  59. [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:nil];
  60. } else {
  61. NSLog(@"Already authenticated!");
  62. }
  63. }
  64. //显示排行榜
  65. - (void) showLeaderboard
  66. {
  67. if (!gameCenterAvailable) return;
  68. GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
  69. if (leaderboardController != nil) {
  70. leaderboardController.leaderboardDelegate = self;
  71. UIWindow *window = [[UIApplication sharedApplication] keyWindow];
  72. currentModalViewController = [[UIViewController alloc] init];
  73. [window addSubview:currentModalViewController.view];
  74. [currentModalViewController presentModalViewController:leaderboardController animated:YES];
  75. }
  76. }
  77. //关闭排行榜回调
  78. - (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController{
  79. if(currentModalViewController !=nil){
  80. [currentModalViewController dismissModalViewControllerAnimated:NO];
  81. [currentModalViewController release];
  82. [currentModalViewController.view removeFromSuperview];
  83. currentModalViewController = nil;
  84. }
  85. }
复制代码

好了。这个来说下如何调用它们吧。

先把AppDelegate.cpp 后缀修改为mm

在AppDelegate.mm里引入

  1. #import "GameKitHelper.h"
复制代码

在 applicationDidFinishLaunching方法里调用,代码写在运行场景之前即可

  1. //GameCenter登陆
  2. [[GameKitHelper sharedGameKitHelper] authenticateLocalUser];
复制代码

到此登陆就ok啦。后面来教大家如何显示排行榜界面

  1. //显示排行榜
  2. [[GameKitHelper sharedGameKitHelper] showLeaderboard];
复制代码

只要把此代码写在你想要调用的方法中就可以了。别忘记了在调用类中引用头文件哦。。

忘记说了。更新一下。。这里需要添加一个 GmaeKit.framework的框架引用。

Cocos2d-X 中文论坛原帖:http://cn.cocos2d-x.org/bbs/forum.php?mod=viewthread&tid=1206


http://bbs.9ria.com/thread-248408-1-1.html

更多的一些内容


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值