本教程基于cocos2d-x 13版本编写 主要内容是告诉大家在c++中如何添加 game center ,找了很多网络上面的代码基本上都是缺胳膊少腿的,代码不完整,对于初学者来说是痛苦的。
本代码仅提供如何登陆GameCenter 显示和关闭Leaderboard(排行榜)。其他实现请参考子龙山人的博客,顺便贴下地址
http://www.cnblogs.com/zilongshanren/archive/2011/06/24/2088383.html 他讲解了很多东西。如何激活GameCenter等值得学习的文章。写的很细致哦废话不说上贴上代码咯
我这里主要如何使用UIViewController 来实现GameCenter的呈现
// // GameKitHelper.h // toDefendTheEarth // // Created by jingjing on 12-6-7. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import <GameKit/GameKit.h> @interface GameKitHelper : NSObject <GKLeaderboardViewControllerDelegate, GKAchievementViewControllerDelegate, GKMatchmakerViewControllerDelegate, GKMatchDelegate>{ BOOL gameCenterAvailable; BOOL userAuthenticated; } @property (assign, readonly) BOOL gameCenterAvailable; + (GameKitHelper *)sharedGameKitHelper; - (void) authenticateLocalUser; - (void) showLeaderboard; - (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController; @end
复制代码
这是实现
好了。这个来说下如何调用它们吧。
先把AppDelegate.cpp 后缀修改为mm
在AppDelegate.mm里引入
#import "GameKitHelper.h"
复制代码
在 applicationDidFinishLaunching方法里调用,代码写在运行场景之前即可
//GameCenter登陆 [[GameKitHelper sharedGameKitHelper] authenticateLocalUser];
复制代码
到此登陆就ok啦。后面来教大家如何显示排行榜界面
//显示排行榜 [[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
更多的一些内容