2010年10月10日星期日

在程序中添加Game Center功能

步骤:

1. 在iTunes Connect中启用Game Center。

2. 配置程序的Bundle identifier

3. 导入Game Kit Framework

4. 导入头文件<GameKit/GameKit.h>

5. 检测当前设备是否支持Game Center

6. 对于不支持Game Center的设备,对Game Kit Framework的引用应改为‘弱引用’(weak link)。

7. 程序启动后,认证用户。

一、iTunes Connect的配置

iTunes Connect Developer Guide

https://itunesconnect.apple.com/docs/iTunesConnect_DeveloperGuide.pdf

二、配置程序的Bundle identifier

com.myCompany.myCoolGame  与 iTunes Connect中配置相同

三、添加FrameWork (weak-link)检测当前设备是否支持Game Center

Target->’Get info’ ->’Genaral’ Linkd libray -> 将Required改为Weak

BOOL isGameCenterAvailable()

{

// Check for presence of GKLocalPlayer API.

Class gcClass = (NSClassFromString(@"GKLocalPlayer"));

// The device must be running 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);

}

四、认证用户

用户必须具有账户才可以访问Game center。程序启动或在需要调用Game Center功能的地方,认证当前的使用账户。

- (void) authenticateLocalUser

{

if([GKLocalPlayer localPlayer].authenticated == NO)

{

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error)

{

if(error == nil)

{

//认证成功

}

else

{

//失败

UIAlertView* alert= [[[UIAlertView alloc] initWithTitle: @"Game Center Account Required"

message: [NSString stringWithFormat: @"Reason: %@", [error localizedDescription]]

delegate: self cancelButtonTitle: @"Try Again..." otherButtonTitles: NULL] autorelease];

[alert show];

}

}

];

}

}

五、排行榜功能

5.1 在iTunes Connect中配置排行榜

(得分格式、排行榜分类)等

5.2 将得分发送到Game Center

- (void) reportScore: (int64_t) score forCategory: (NSString*) category

{

GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];

scoreReporter.value = score;

[scoreReporter reportScoreWithCompletionHandler: ^(NSError *error)

{

if(error != nil)

{

//处理储物

}

else

{

//显示排行榜

}

}];

}

5.3 显示排行榜(LeaderboardController)

- (void) showLeaderboard

{

GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];

if (leaderboardController != nil) {

leaderboardController.leaderboardDelegate = self;

[self presentModalViewController: leaderboardController animated: YES];

}

}

在显示leaderboard之前,可以配置leaderboard view Controller的一些属性。(category 属性,配置显示哪中分类下的排行榜;timeScope属性,配置显示哪个时间段内的排行榜)

//响应用户关闭排行榜的事件:

-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController

{

[self dismissModalViewControllerAnimated:YES];

}

5.4 获取排行榜数据,自定义排行榜显示的view

//获取所有用户

- (void) retrieveTopTenScores

{

GKLeaderboard *leaderboardRequest = [GKLeaderboard alloc] init];

if (leaderboardRequest != nil) {

leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;

leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;

leaderboardRequest.range = NSMakeRange(1,10);

[leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {

if (error != nil)

// handle the error.

if (scores != nil)

// process the score information.

}];

}

//获取特定用户的排行榜数据

- (void) receiveMatchBestScores: (GKMatch*) match

{

GKLeaderboard *query = [GKLeaderboard alloc] initWithPlayers: match.players];

if (query != nil)

{

[query loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {

}

if (error != nil) // handle the error.

if (scores != nil) // process the score information.

}];

}

六、Cocos2d 中加入 Game Center

6.1 方式1 (直接获取数据、自定义排行榜的view)

6。2 0.99.5 sdk of cocos2d have a RootViewController

6.3 方式3  使用viewcontroller

创建一个viewcontroller 将其view属性添加到cocos2d的 glview


UIViewController *tempVC=[[UIViewController alloc] init];

GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];

request.minPlayers = 2;

request.maxPlayers = 2;

request.playersToInvite = playersToInvite;

GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];

mmvc.matchmakerDelegate = self;

[[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];

[tempVC presentModalViewController:mmvc animated:YES];

//响应事件

-(void)matchmakerViewControllerWasCancelled:(GKMatchmakerViewController *)viewController

{

[tempVC dismissModalViewControllerAnimated:YES];

[tempVC.view removeFromSuperview];

}

//退出时

-(void) onExit

{

[super onExit];

[tempVC release];

}

- (void) showLeaderboard

{

GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];

if (leaderboardController != nil)

{

leaderboardController.leaderboardDelegate = self;

[[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];

[tempVC presentModalViewController:leaderboardController animated: YES];

}

}

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController

{

[tempVC dismissModalViewControllerAnimated:YES];

[tempVC.view removeFromSuperview];

}

步骤:1. 在iTunes Connect中启用Game Center。2. 配置程序的Bundle identifier3. 导入Game Kit Framework4. 导入头文件<GameKit/GameKit.h>5. 检测当前设备是否支持Game Center6. 对于不支持Game Center的设备,对Game Kit Framework的引用应改为‘弱引用’(weak link)。7. 程序启动后,认证用户。
一、iTunes Connect的配置    iTunes Connect Developer Guide  https://itunesconnect.apple.com/docs/iTunesConnect_DeveloperGuide.pdf\r
二、配置程序的Bundle identifier
com.myCompany.myCoolGame  与 iTunes Connect中配置相同
三、添加FrameWork (weak-link)检测当前设备是否支持Game Center
Target->’Get info’ ->’Genaral’ Linkd libray -> 将Required改为Weak
BOOL isGameCenterAvailable(){
// Check for presence of GKLocalPlayer API. Class gcClass = (NSClassFromString(@"GKLocalPlayer"));// The device must be running running iOS 4.1 or later.
NSString *reqSysVer = @"4.1"; NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; BOOL osVersionSupported = ([currSysVer compare:reqSysVeroptions:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);}















四、认证用户用户必须具有账户才可以访问Game center。程序启动或在需要调用Game Center功能的地方,认证当前的使用账户。
- (void) authenticateLocalUser{ if([GKLocalPlayer localPlayer].authenticated == NO) { [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error)  { if(error == nil) { //认证成功 } else { //失败UIAlertView* alert= [[[UIAlertView alloc] initWithTitle: @"Game Center Account Required"  message: [NSString stringWithFormat: @"Reason: %@", [error localizedDescription]] delegate: self cancelButtonTitle: @"Try Again..." otherButtonTitles: NULL] autorelease]; [alert show]; } } ]; }}

五、排行榜功能5.1 在iTunes Connect中配置排行榜(得分格式、排行榜分类)等5.2 将得分发送到Game Center- (void) reportScore: (int64_t) score forCategory: (NSString*) category { GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease]; scoreReporter.value = score; [scoreReporter reportScoreWithCompletionHandler: ^(NSError *error)  { if(error != nil) {    //处理储物 }  else      {        //显示排行榜 } }];}


5.3 显示排行榜(LeaderboardController)- (void) showLeaderboard{GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil) { leaderboardController.leaderboardDelegate = self;  [self presentModalViewController: leaderboardController animated: YES]; }}


在显示leaderboard之前,可以配置leaderboard view Controller的一些属性。(category 属性,配置显示哪中分类下的排行榜;timeScope属性,配置显示哪个时间段内的排行榜)

//响应用户关闭排行榜的事件:
-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController  { [self dismissModalViewControllerAnimated:YES];  }


5.4 获取排行榜数据,自定义排行榜显示的view
//获取所有用户- (void) retrieveTopTenScores{ GKLeaderboard *leaderboardRequest = [GKLeaderboard alloc] init]; if (leaderboardRequest != nil) { leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal; leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime; leaderboardRequest.range = NSMakeRange(1,10); [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) { if (error != nil)  // handle the error. if (scores != nil) // process the score information.}];}
//获取特定用户的排行榜数据- (void) receiveMatchBestScores: (GKMatch*) match{ GKLeaderboard *query = [GKLeaderboard alloc] initWithPlayers: match.players]; if (query != nil) { [query loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {} if (error != nil) // handle the error. if (scores != nil) // process the score information.}];}
六、Cocos2d 中加入 Game Center

6.1 方式1 (直接获取数据、自定义排行榜的view)

6。2 0.99.5 sdk of cocos2d have a RootViewController

6.3 方式3  使用viewcontroller创建一个viewcontroller 将其view属性添加到cocos2d的 glview

UIViewController *tempVC=[[UIViewController alloc] init];

GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];

request.minPlayers = 2;request.maxPlayers = 2;

request.playersToInvite = playersToInvite;

GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];

mmvc.matchmakerDelegate = self;

[[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];

[tempVC presentModalViewController:mmvc animated:YES];

//响应事件

-(void)matchmakerViewControllerWasCancelled:(GKMatchmakerViewController *)viewController

{

[tempVC dismissModalViewControllerAnimated:YES];[tempVC.view removeFromSuperview];}
//退出时-(void) onExit

{

[super onExit];

[tempVC release];

}



- (void) showLeaderboard{GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];

if (leaderboardController != nil)

{ leaderboardController.leaderboardDelegate = self;

[[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];

[tempVC presentModalViewController:leaderboardController animated: YES];

}

}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController

{

[tempVC dismissModalViewControllerAnimated:YES];

[tempVC.view removeFromSuperview];

}
发表评论