中国法律网
法律通行证: 用户名: 密码:  注册
律师加盟热线:400-8919-913   律信通 律信通  
律师
公众 咨询 贴吧
律信通 案件委托
频道 房产 婚姻 交通事故 保险 建设工程 劳动
留学
公司 合同 刑事辩护 医疗 知识产权 工商
新闻 宽频 文书 常识 案例
法规 专题 杂志 百科 论文
查找全国各地律师: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 点击各城市名拼音首字母查找律师 公众找律师,信赖律信通!律师做宣传,首选律信通!
当前位置: 主页 > 法律专题2 > 申请的格式 >

【iOS开发必备指南合集】申请企业级IDP、真机调试、游戏接入Game(2)

时间:2013-08-15 00:08来源:互联网 作者:admin 点击:
4.2用户登录 - (void) authenticateLocalPlayer{[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error){if (error == nil) {//成功处理NSLog(@"成功");NSLog(@"1--alias--.%@",[GKLoc

4.2用户登录

- (void) authenticateLocalPlayer { [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error){ if (error == nil) { //成功处理 NSLog(@"成功"); NSLog(@"1--alias--.%@",[GKLocalPlayer localPlayer].alias); NSLog(@"2--authenticated--.%d",[GKLocalPlayer localPlayer].authenticated); NSLog(@"3--isFriend--.%d",[GKLocalPlayer localPlayer].isFriend); NSLog(@"4--playerID--.%@",[GKLocalPlayer localPlayer].playerID); NSLog(@"5--underage--.%d",[GKLocalPlayer localPlayer].underage); }else { //错误处理 NSLog(@"失败 %@",error); } }]; }

对于开发者来说,Game Center必须经过测试才能上线,没有上线的程序在测试环境中登录时会出现sandbox提示.如图.

4.3用户变更检测
由于4.0以后系统支持多任务,玩家的机器有可能被不同的玩家接触,导致Game Center上的用户发生变化,当发生变化的时候,程序必须在游戏中通知玩家.

- (void) registerForAuthenticationNotification { NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(authenticationChanged) name:GKPlayerAuthenticationDidChangeNotificationName object:nil]; } - (void) authenticationChanged { if ([GKLocalPlayer localPlayer].isAuthenticated) { ;// Insert code here to handle a successful authentication. } else { ;// Insert code here to clean up any outstanding Game Center-related classes. } }

5.对Leaderboard进行操作
5.1上传一个分数

- (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) { // handle the reporting error NSLog(@"上传分数出错."); //If your application receives a network error, you should not discard the score. //Instead, store the score object and attempt to report the player’s process at //a later time. }else { NSLog(@"上传分数成功"); } }]; }

当上传分数出错的时候,要将上传的分数存储起来,比如将SKScore存入一个NSArray中.等可以上传的时候再次尝试.

5.2下载一个分数

//GKScore objects provide the data your application needs to create a custom view. //Your application can use the score object’s playerID to load the player’s alias. //The value property holds the actual value you reported to Game Center. the formattedValue //property provides a string with the score value formatted according to the parameters //you provided in iTunes Connect. - (void) retrieveTopTenScores { GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init]; if (leaderboardRequest != nil) { leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal; leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime; leaderboardRequest.range = NSMakeRange(1,10); leaderboardRequest.category = @"TS_LB"; [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) { if (error != nil){ // handle the error. NSLog(@"下载失败"); } if (scores != nil){ // process the score information. NSLog(@"下载成功...."); NSArray *tempScore = [NSArray arrayWithArray:leaderboardRequest.scores]; for (GKScore *obj in tempScore) { NSLog(@" playerID : %@",obj.playerID); NSLog(@" category : %@",obj.category); NSLog(@" date : %@",obj.date); NSLog(@" formattedValue : %@",obj.formattedValue); NSLog(@" value : %d",obj.value); NSLog(@" rank : %d",obj.rank); NSLog(@"**************************************"); } } }]; } }

说明:
1) playerScope:表示检索玩家分数范围.
2) timeScope:表示某一段时间内的分数
3) range:表示分数排名的范围
4) category:表示你的Leaderboard的ID.

5.3玩家信息交互
Game Center最重要的一个功能就是玩家交互.所以,必须检索已经登录玩家的好友信息.根据自己的需要做出设置,比如,可以与好友比较分数,或者好友排行榜等.
//检索已登录用户好友列表

- (void) retrieveFriends { GKLocalPlayer *lp = [GKLocalPlayer localPlayer]; if (lp.authenticated) { [lp loadFriendsWithCompletionHandler:^(NSArray *friends, NSError *error) { if (error == nil) { [self loadPlayerData:friends]; } else { ;// report an error to the user. } }]; } }

上面的friends得到的只是一个身份列表,里面存储的是NSString,想要转换成好友ID,必须调用- (void) loadPlayerData: (NSArray *) identifiers方法,该方法得到的array里面存储的才是GKPlayer对象.如下

/* Whether you received player identifiers by loading the identifiers for the local player’s friends, or from another Game Center class, you must retrieve the details about that player from Game Center. */ - (void) loadPlayerData: (NSArray *) identifiers { [GKPlayer loadPlayersForIdentifiers:identifiers withCompletionHandler:^(NSArray *players, NSError *error) { if (error != nil) { // Handle the error. } if (players != nil) { NSLog(@"得到好友的alias成功"); GKPlayer *friend1 = [players objectAtIndex:0]; NSLog(@"friedns---alias---%@",friend1.alias); NSLog(@"friedns---isFriend---%d",friend1.isFriend); NSLog(@"friedns---playerID---%@",friend1.playerID); } }]; }

至此,leaderboard功能介绍完毕

(责任编辑:admin)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
用户名: 验证码: 点击我更换图片
栏目列表
推荐内容
中国法律网 版权所有 邮箱:service@5Law.cn 建议使用:1024x768分辨率,16位以上颜色 | 京ICP备2023040428号-1联系我们 有事点这里    [切换城市▲] 公司法
400-8919-913 工作日:9:00-18:00
周 六:9:00-12:00

法律咨询5分钟内回复
请用微信扫描二维码
关闭

关注网站CEO微信,与CEO对话