课程表

iOS课程

工具箱
速查手册

GameKit

当前位置:免费教程 » 移动开发 » iOS

简介

GameKit是iOS SDK中一个常用的框架。其核心功能有3个:

  • 交互游戏平台Game Center,
  • P2P设备通讯功能
  • In-Game Voice。

实例步骤

1.在链接 iTunes 时请确保拥有一个唯一的 App ID( unique App ID),App ID在我们应用程序更新 bundle ID时及在Xcode代码签名与相应的配置文件需要使用到。

2.创建新的应用程序和更新应用程序信息。在添加新的应用程序文档可以了解更多有关信息。

3.打开你申请的application,点击Manage Game Center选项。进入后点击Enable Game Center使你的Game Center生效。接下来设置自己的Leaderboard和Achievements。

4.下一步涉及处理代码,并为我们的应用程序创建用户界面。

5.创建一个single view application,并输入 bundle identifier 。

6.更新 ViewController.xib,如下所示

gamekitInterface

7.选择项目文件,然后选择目标,然后添加GameKit.framework

8.为已添加的按钮创建IBActions

9.更新ViewController.h文件,如下所示

  1. #import <UIKit/UIKit.h>
  2. #import <GameKit/GameKit.h>
  3.  
  4. @interface ViewController : UIViewController
  5. <GKLeaderboardViewControllerDelegate>
  6.  
  7. -(IBAction)updateScore:(id)sender;
  8. -(IBAction)showLeaderBoard:(id)sender;
  9.  
  10. @end

10.更新ViewController.m ,如下所示

  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()
  4.  
  5. @end
  6.  
  7. @implementation ViewController
  8.  
  9. - (void)viewDidLoad
  10. {
  11. [super viewDidLoad];
  12. if([GKLocalPlayer localPlayer].authenticated == NO)
  13. {
  14. [[GKLocalPlayer localPlayer]
  15. authenticateWithCompletionHandler:^(NSError *error)
  16. {
  17. NSLog(@"Error%@",error);
  18. }];
  19. }
  20. }
  21.  
  22. - (void)didReceiveMemoryWarning
  23. {
  24. [super didReceiveMemoryWarning];
  25. // Dispose of any resources that can be recreated.
  26. }
  27. - (void) updateScore: (int64_t) score
  28. forLeaderboardID: (NSString*) category
  29. {
  30. GKScore *scoreObj = [[GKScore alloc]
  31. initWithCategory:category];
  32. scoreObj.value = score;
  33. scoreObj.context = 0;
  34. [scoreObj reportScoreWithCompletionHandler:^(NSError *error) {
  35. // Completion code can be added here
  36. UIAlertView *alert = [[UIAlertView alloc]
  37. initWithTitle:nil message:@"Score Updated Succesfully"
  38. delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
  39. [alert show];
  40.  
  41. }];
  42. }
  43. -(IBAction)updateScore:(id)sender{
  44. [self updateScore:200 forLeaderboardID:@"tutorialsPoint"];
  45. }
  46. -(IBAction)showLeaderBoard:(id)sender{
  47. GKLeaderboardViewController *leaderboardViewController =
  48. [[GKLeaderboardViewController alloc] init];
  49. leaderboardViewController.leaderboardDelegate = self;
  50. [self presentModalViewController:
  51. leaderboardViewController animated:YES];
  52.  
  53. }
  54. #pragma mark - Gamekit delegates
  55. - (void)leaderboardViewControllerDidFinish:
  56. (GKLeaderboardViewController *)viewController{
  57. [self dismissModalViewControllerAnimated:YES];
  58. }
  59.  
  60. @end

输出

运行该应用程序,输出结果如下

gamekit_Output1

当我们单击显示排行榜时,屏幕显示如下:

gamekit_Output2

当我们点击更新分数,比分将被更新到我们排行榜上,我们会得到一个信息,如下图所示

gamekit_Output3

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号