寫這篇文章的主要用於給自己加強印象,幫助他人就是幫助自己.
本教程基於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
這是實現
//
// GameKitHelper.m
// toDefendTheEarth
//
// Created by jingjing on 12-6-7.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "GameKitHelper.h"
@implementation GameKitHelper
@synthesize gameCenterAvailable;
//靜態初始化 對外接口
static GameKitHelper *sharedHelper = nil;
static UIViewController* currentModalViewController = nil;
+ (GameKitHelper *) sharedGameKitHelper {
if (!sharedHelper) {
sharedHelper = [[GameKitHelper alloc] init];
}
return sharedHelper;
}
//用於驗證
- (BOOL)isGameCenterAvailable {
// check for presence of GKLocalPlayer API
Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
// check if the device is running iOS 4.1 or later
NSString *reqSysVer =@"4.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer
options:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);
}
- (id)init {
if ((self = [super init])) {
gameCenterAvailable = [self isGameCenterAvailable];
if (gameCenterAvailable) {
NSNotificationCenter *nc =
[NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(authenticationChanged)
name:GKPlayerAuthenticationDidChangeNotificationName
object:nil];
}
}
return self;
}
//后台回調登陸驗證
- (void)authenticationChanged {
if ([GKLocalPlayer localPlayer].isAuthenticated &&!userAuthenticated) {
NSLog(@"Authentication changed: player authenticated.");
userAuthenticated = TRUE;
} else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) {
NSLog(@"Authentication changed: player not authenticated");
userAuthenticated = FALSE;
}
}
- (void)authenticateLocalUser {
if (!gameCenterAvailable) return;
NSLog(@"Authenticating local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO) {
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:nil];
} else {
NSLog(@"Already authenticated!");
}
}
//顯示排行榜
- (void) showLeaderboard
{
if (!gameCenterAvailable) return;
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil) {
leaderboardController.leaderboardDelegate = self;
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
currentModalViewController = [[UIViewController alloc] init];
[window addSubview:currentModalViewController.view];
[currentModalViewController presentModalViewController:leaderboardController animated:YES];
}
}
//關閉排行榜回調
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController{
if(currentModalViewController !=nil){
[currentModalViewController dismissModalViewControllerAnimated:NO];
[currentModalViewController release];
[currentModalViewController.view removeFromSuperview];
currentModalViewController = nil;
}
}
好了。這個來說下如何調用它們吧。
先把AppDelegate.cpp 后綴修改為mm
在AppDelegate.mm里引入
#import "GameKitHelper.h"
在 applicationDidFinishLaunching方法里調用,代碼寫在運行場景之前即可
//GameCenter登陸 [[GameKitHelper sharedGameKitHelper] authenticateLocalUser];
到此登陸就ok啦。后面來教大家如何顯示排行榜界面
//顯示排行榜 [[GameKitHelper sharedGameKitHelper] showLeaderboard];
只要把此代碼寫在你想要調用的方法中就可以了。別忘記了在調用類中引用頭文件哦。。
忘記說了。更新一下。。這里需要添加一個 GmaeKit.framework的框架引用。
over
