一些startBrowsingForNearbyPlayersWithReachableHandler问题

Hen*_*oke 5 gamekit ios game-center

我正试图让GameKit使用本地配对[[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:].基本上,我正在尝试实现无接口的本地匹配:只要我本地附近有一个玩家,我想连接并开始匹配.重要的是,我只是想为本土球员做到这一点:我从来没有想在互联网上自动匹配.

我在iTunes iTunes中为我的应用启用了Game Center,并在我用来测试的每台设备上注册了一个不同的沙盒帐户.

我尝试了两种配对GKMatchmakerViewController(在验证本地播放器之后)和程序化配对startBrowsingForNearbyPlayersWithReachableHandler:,在iPhone 4和第四代iPod Touch上运行相同的代码在我的桌面上彼此相邻.都没有找到另一个; 当使用GKMatchmakerViewController界面寻找附近的球员仍然在

寻找玩家......

微调器,使用时startBrowsingForNearbyPlayersWithReachableHandler:,通知块永远不会被调用.

这是我目前的测试代码块:

-(void)connectLocal
{
    if( ![GKLocalPlayer localPlayer].isAuthenticated )
    {
        // authenticateLocalPlayer calls connectLocal again after authentication is complete
        [ self authenticateLocalPlayer ];
        return;
    }
    [[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:^(NSString *playerID, BOOL reachable) {
             NSLog(@"Reachability changed for player %@", playerID );
        } ];
}
Run Code Online (Sandbox Code Playgroud)

文档在这个主题上有点稀疏和混乱,特别是当涉及到本地多人游戏和互联网上的比赛之间的差异时.例如,在找到加入该匹配的玩家之前,似乎有必要对本地玩家进行身份验证并创建匹配(创建任何匹配以匹配请求开始).然而,这个小金块似乎暗示:

标准用户界面允许玩家发现附近的其他玩家,即使这两个玩家当前都没有直接连接到Game Center.

另外,在搜索附近玩家中描述的流程中,通过传递给通知块的玩家找到玩家之后,直到步骤3才创建匹配请求startBrowsingForNearbyPlayersWithReachableHandler:.不幸的是,我从来没有这么远.

所以,问题:

1)我认为我可以startBrowsingForNearbyPlayersWithReachableHandler:在认证本地玩家之前打电话是对的吗?GameKit没有抛出错误,所以我假设它没问题.这可能是一个轻率的假设.我是否认证似乎并没有太大的区别.

2)有没有人成功实现了本地自动匹配[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:?是否有好的示例代码可以说明完整的流程,从浏览播放器到开始匹配,都是以编程方式进行的?

3)关于是否可以在iOS模拟器中测试启用GameKit的应用程序,网上似乎存在相互矛盾的报道.普遍的共识似乎没有,并且最好在iOS硬件上进行测试.我一直在使用iPhone 4和第四代iPod Touch.对于那些已成功测试本地多人游戏的人,您使用了哪些测试设置和方法?

Sha*_*ram 1

您需要按以下顺序执行这些操作:

  1. 验证本地玩家
  2. 安装邀请处理程序
  3. 开始浏览附近的玩家

需要身份验证 - 这会向 Game Center 注册您的应用程序并让玩家登录。在大多数情况下,您甚至不需要访问互联网即可执行此操作。

还需要安装邀请处理程序,我认为这是您缺少的步骤。这可以让您的应用知道收到入站​​邀请时要做什么。如果您不这样做,设备将不会注册为位于附近。

完成上述两项操作后才可以开始浏览。

这里有一些示例代码可以帮助您继续。应用程序启动后调用此方法:

- (void) authenticateLocalPlayer
{

    static BOOL gcAuthenticationCalled = NO;
    if (!gcAuthenticationCalled) {
        GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

        void (^authenticationHandler)(UIViewController*, NSError*) = ^(UIViewController *viewController, NSError *error) {
            NSLog(@"Authenticating with Game Center.");
            GKLocalPlayer *myLocalPlayer = [GKLocalPlayer localPlayer];
            if (viewController != nil)
            {
                NSLog(@"Not authenticated - storing view controller.");
                self.authenticationController = viewController;
            }
            else if ([myLocalPlayer isAuthenticated])
            {
                NSLog(@"Player is authenticated!");

                //iOS8 - register as a listener
                [localPlayer unregisterAllListeners];
                [localPlayer registerListener:self];

                [[GKLocalPlayer localPlayer] loadFriendPlayersWithCompletionHandler:^(NSArray *friendPlayers, NSError *error) {

                    //Do something with the friends

                }];

                //iOS7 - install an invitation handler
                [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
                    // Insert game-specific code here to clean up any game in progress.
                    if (acceptedInvite)
                    {
                        //This player accepted an invitation.
                        //If doing programmatic matchmaking, call GKMatchmaker's matchForInvite:completionHandler 
                        //to get a match for the invite.  Otherwise you need to allocate a GKMatchmakerViewController 
                        //instance and present it with the invite.

                    }
                    else if (playersToInvite)
                    {
                        //Your game was launched from the GameCenter app to host a match.
                    }
                };

                //Now you can browse.  Note this is the iOS8 call.  The iOS7 call is slightly different.
                [[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithHandler:^(GKPlayer *player, BOOL reachable) {

                    NSLog(@"Player Nearby: %@", player.playerID);

                }];



            }
            else
            {
                //Authentication failed.
                self.authenticationController = nil;
                if (error) {
                    NSLog([error description], nil);
                }
            }


        };

        localPlayer.authenticateHandler = authenticationHandler;
        gcAuthenticationCalled = YES;
    }
}
Run Code Online (Sandbox Code Playgroud)

* 重要 * 如果您使用的是 iOS8,则无需安装邀请处理程序。您可以将一个对象注册为侦听协议 GKLocalPlayerListener,并实现以下方法:

-player:didAcceptInvite:
-player:didRequestMatchWithRecipients:
Run Code Online (Sandbox Code Playgroud)

如果在iOS8上不实现这些方法,是不行的!

然后,您可以在验证本地玩家身份后调用此函数,将 GKMatchmaker 链接到该对象:

[localPlayer registerListener:self];
Run Code Online (Sandbox Code Playgroud)

确保实现协议的对象在 .h 文件中声明如下:

@interface MyImplementingObject : NSObject <GKMatchDelegate, GKLocalPlayerListener>
Run Code Online (Sandbox Code Playgroud)

如果您执行了所有这些操作但仍然无法正常工作,请确保您在应用程序中正确设置了捆绑包 ID(单击应用程序,单击“目标”,确保填写了捆绑包标识符和版本),然后单击“功能”选项卡 (XCode 6),并确保 Game Center 已打开。

转到会员中心并确保使用该捆绑包 ID 的应用程序也为其配置文件启用了 Game Center。如有必要,下载并重新应用您的配置文件。

确保 GameCenter 下的“设置”中的沙盒开关处于打开状态,并确保“允许邀请”和“附近玩家”开关处于打开状态。

最后,请确保您转到 iTunes Connect 并验证您的应用程序也已启用 Game Center。