IOS GameKit


简介

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文件,如下所示

#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>

@interface ViewController : UIViewController
<GKLeaderboardViewControllerDelegate>

-(IBAction)updateScore:(id)sender;
-(IBAction)showLeaderBoard:(id)sender;

@end

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

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    if([GKLocalPlayer localPlayer].authenticated == NO)
    {
      [[GKLocalPlayer localPlayer] 
      authenticateWithCompletionHandler:^(NSError *error)
      {
         NSLog(@"Error%@",error);
      }];
    }    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void) updateScore: (int64_t) score 
forLeaderboardID: (NSString*) category
{
    GKScore *scoreObj = [[GKScore alloc]
    initWithCategory:category];
    scoreObj.value = score;
    scoreObj.context = 0;
    [scoreObj reportScoreWithCompletionHandler:^(NSError *error) {
        // Completion code can be added here
        UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle:nil message:@"Score Updated Succesfully" 
        delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        ;

    }];
}
-(IBAction)updateScore:(id)sender{
    [self updateScore:200 forLeaderboardID:@"runoob"];
}
-(IBAction)showLeaderBoard:(id)sender{
    GKLeaderboardViewController *leaderboardViewController =
    [[GKLeaderboardViewController alloc] init];
    leaderboardViewController.leaderboardDelegate = self;
    [self presentModalViewController:
    leaderboardViewController animated:YES];

}
#pragma mark - Gamekit delegates
- (void)leaderboardViewControllerDidFinish:
(GKLeaderboardViewController *)viewController{
    [self dismissModalViewControllerAnimated:YES];
}

@end

输出

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

gamekit_Output1

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

gamekit_Output2

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

gamekit_Output3

若文章对你有帮助,可以点赞或打赏支持我们。发布者:lyh会员,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/ios-gamekit/

(0)
lyhlyh会员认证作者
上一篇 2023年 4月 1日 下午9:06
下一篇 2023年 4月 1日 下午9:10

相关推荐

  • partprobe

    文章目录partprobe补充说明语法选项参数实例 partprobe 不重启的情况下重读分区 补充说明 partprobe命令 用于重读分区表,当出现删除文件后,出现仍然占用空间…

    入门教程 2024年 3月 1日
  • createrepo

    文章目录createrepo概要描述选项返回值例子 createrepo 创建YUM仓库 概要 createrepo [选项] <目录> 描述 createrepo是一…

    入门教程 2023年 12月 7日
  • ssh-keyscan

    文章目录ssh-keyscan补充说明语法选项参数 ssh-keyscan 收集主机公钥的使用工具 补充说明 ssh-keyscan命令 是一个收集大量主机公钥的使用工具。 语法 …

    入门教程 2024年 3月 11日
  • uniq

    文章目录uniq概要主要用途选项参数返回值例子注意 uniq 显示或忽略重复的行。 概要 uniq [OPTION]… [INPUT [OUTPUT]] 主要用途 将输入文件(…

    入门教程 2024年 3月 11日
  • chkconfig

    文章目录chkconfig补充说明语法选项实例 chkconfig 检查或设置系统的各种服务 补充说明 chkconfig命令 检查、设置系统的各种服务。这是Red Hat公司遵循…

    入门教程 2023年 12月 7日
  • HTML 属性

    属性是 HTML 元素提供的附加信息。 文章目录HTML 属性属性实例实例HTML 属性常用引用属性值HTML 提示:使用小写属性HTML 属性参考手册 HTML 属性 HTML …

    2023年 4月 11日
  • ip6tables-restore

    文章目录ip6tables-restore补充说明语法选项 ip6tables-restore 还原ip6tables表 补充说明 ip6tables-restore命令 用来还原…

    入门教程 2023年 12月 19日
  • ld

    文章目录ld补充说明语法选项参数实例 ld 将目标文件连接为可执行程序 补充说明 ld命令 是GNU的连接器,将目标文件连接为可执行程序。 语法 ld(选项)(参数) ld [op…

    入门教程 2023年 12月 19日
  • SSH 连接出现 Connection reset by peer 如何解决

    以上错误一般是因为以下原因导致: 服务器改了密码,试过密码多次后出现 服务器安全策略,无法下发ssh-key-id 调试方法一般是 ssh -v root@ip_addr 文章目录…

    2021年 8月 6日
  • HTML 段落

    HTML 可以将文档分割为若干段落。 文章目录HTML 段落实例不要忘记结束标签HTML 折行实例HTML 输出- 使用提醒HTML 标签参考手册 HTML 段落 段落是通过 &l…

    2023年 4月 11日
Translate »