一些startBrowsingForNearbyPlayersWithReachableHandler问题 [英] Some startBrowsingForNearbyPlayersWithReachableHandler questions

查看:92
本文介绍了一些startBrowsingForNearbyPlayersWithReachableHandler问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用[[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:]在GameKit中进行本地配对.本质上,我正在尝试实现无界面的本地比赛:只要我附近有一个球员,我就想连接并开始比赛.重要的是,我要为本地玩家执行此操作:我从不希望通过互联网自动进行比赛.

I'm trying to get local matchmaking working in GameKit using [[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:]. Essentially, I'm trying to implement interface-less local matches: as long as there's a player in my local vicinity, I want to connect and start a match. Importantly, I only want to do this for local players: I never want to match automatically over the internet.

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

I've enabled Game Center for my app in iTunes connect and signed up for a different sandbox account on every device I'm using to test.

我已经尝试过使用GKMatchmakerViewController进行配对,并使用startBrowsingForNearbyPlayersWithReachableHandler:进行程序配对,在iPhone 4和第4代iPod Touch上并排坐在我的桌子上运行相同的代码.双方都找不到.使用GKMatchmakerViewController时,用于查找附近玩家的界面仍位于

I've tried both matchmaking with GKMatchmakerViewController (after authenticating the local player) and programmatic matchmaking with startBrowsingForNearbyPlayersWithReachableHandler:, running the same code on an iPhone 4 and an 4th gen iPod Touch sitting next to each other on my desk. Neither ever finds the other; when using GKMatchmakerViewController the interface for finding nearby players remains at the

寻找玩家...

Finding Players...

spinner,并且在使用startBrowsingForNearbyPlayersWithReachableHandler:时,通知块永远不会被调用.

spinner, and when using startBrowsingForNearbyPlayersWithReachableHandler:, the notification block never gets called.

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

Here's my current block of testing code:

-(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 );
        } ];
}

文档有些稀疏,在主题上造成混淆,尤其是当涉及本地多人播放器和互联网上的比赛之间的区别时.例如,在找到要加入该比赛的球员之前,似乎有必要对本地球员进行身份验证并创建比赛(这个小金块似乎暗示了其他问题:

The docs are a little sparse & confusing on the subject, especially when it comes to the difference between local mulitplayer and matches over the internet. For instance, it seems to be necessary to authenticate the local player and create a match before finding players to join that match (Creating Any Kind of Match Starts with a Match Request). However this little nugget seems to suggest otherwise:

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

The standard user interface allows players to discover other nearby players, even when neither player is currently connected to Game Center directly.

此外,在

Additionally, in the flow described in Searching For Nearby Players, a match request isn't created until step 3, after finding players via the notification block passed to startBrowsingForNearbyPlayersWithReachableHandler:. Unfortunately, I've never got that far.

那么,这些问题:

1)我是否认为可以在对本地播放器进行身份验证之前致电startBrowsingForNearbyPlayersWithReachableHandler:? GameKit不会引发错误,因此我假设它没问题.这可能是轻率的假设.我是否进行身份验证似乎并没有多大区别.

1) Am I right in thinking I can call startBrowsingForNearbyPlayersWithReachableHandler: before authenticating the local player? GameKit doesn't throw an error, so I'm assuming it's OK. This may be a rash assumption. Whether I authenticate or not doesn't seem to make much difference.

2)是否有人成功使用[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:实现了本地自动匹配?哪里有很好的示例代码来说明完整的流程,从浏览球员到比赛开始,全部都以编程方式完成?

2) Has anyone successfully implemented local auto-matching using [GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:? Is there good example code anywhere that illustrates the complete flow, from browsing for players to starting a match, all programmatically?

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

3) There seem to be conflicting reports on the web over whether GameKit-enabled apps can be tested in the iOS Simulator. General consensus seems not, and it's better to test on iOS hardware. I've been using a iPhone 4 & an 4th gen iPod Touch. For those who have successfully tested local multiplayer, what testing setup & methodology did you use?

推荐答案

您需要按以下顺序进行操作:

You need to do these things in this order:

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

需要身份验证-这会在Game Center中注册您的应用并登录玩家.在大多数情况下,您甚至都不需要Internet即可执行此操作.

Authentication is required - this registers your app with Game Center and logs the player in. In most cases, you won't even need internet access to do this.

还需要安装邀请处理程序,我认为这是您所缺少的步骤.这使您的应用程序知道收到入站​​邀请时该怎么办.如果您不这样做,则设备不会注册为附近的设备.

Installing the invitation handler is also required, and I think this is the step you're missing. This lets your app know what to do when an inbound invitation is received. If you don't do this, a device won't register as being nearby.

完成以上两个操作后,才开始浏览.

Only start browsing once you've done the above two.

以下是一些示例代码,可以助您一臂之力.应用启动后调用此方法:

Here's some sample code to get you going. Call this method after the app launches:

- (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;
    }
}

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

* IMPORTANT * If you're using iOS8, you don't install the invitation handler. You instead register an object as listening for the protocol GKLocalPlayerListener, and implement these methods:

-player:didAcceptInvite:
-player:didRequestMatchWithRecipients:

如果您未在iOS8上实现这些方法,将无法正常工作!

然后在验证本地播放器后通过调用GKMatchmaker将该对象链接到该对象:

You then link GKMatchmaker to that object by calling this after authenticating the local player:

[localPlayer registerListener:self];

确保在.h文件中像这样声明了实现协议的对象:

Make sure the object that's implementing the protocol is declared like so in the .h file:

@interface MyImplementingObject : NSObject <GKMatchDelegate, GKLocalPlayerListener>

如果您执行了所有这些操作后仍然无法使用,请确保在应用程序中正确设置了捆绑软件ID(单击应用程序,单击目标",确保已填写捆绑软件标识符和版本),然后点击功能"标签(XCode 6),并确保打开了Game Center.

If you do all this and it still isn't working, make sure that you have your bundle ID set correctly in your app (Click the app, click 'Targets', make sure Bundle Identifier and Version are filled in), then click the 'Capabilities' tab (XCode 6), and make sure Game Center is on.

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

Go to the Member Center and make sure that the app using that bundle ID also has Game Center enabled for its Provisioning Profile. Download and reapply your Provisioning Profile if necessary.

确保在GameCenter下的设置"中将沙箱开关打开,并确保将允许邀请"和附近的玩家"开关打开.

Make sure the sandbox switch is ON in your Settings under GameCenter, and also make sure that 'Allow Invites' and 'Nearby Players' switches are turned ON.

最后,确保您转到iTunes Connect并确认那里的应用程序也启用了Game Center.

Finally, make sure you go to iTunes Connect and verify that Game Center is enabled for your app there as well.

这篇关于一些startBrowsingForNearbyPlayersWithReachableHandler问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆