【问题标题】:Game Center Multiplayer using GKMatch but seems can't be connectedGame Center Multiplayer 使用 GKMatch 但似乎无法连接
【发布时间】:2010-12-02 16:45:03
【问题描述】:

大家好,我是 iOS 版 Game Center 的新手。我正在尝试将多人游戏功能添加到我的游戏中并遵循文档。

到目前为止,我的 2 个客户可以成功获得匹配,即调用 matchmakerViewController:didFindMatch 回调并传递 GKMatch 对象。

但是在那之后我似乎永远被困在那里,因为根据文档,我必须等到所有玩家(在我的例子中是 2 个)真正连接后才能开始我的游戏。但似乎从未调用 match:player:didChangeState 回调来指示连接成功。好吧,我确定我的客户都在同一个 wifi 网络中(或者它是必须的吗?)有人可以在这种情况下启发我吗?我是否需要做任何额外的事情才能使客户端连接?非常感谢您的帮助!

【问题讨论】:

  • 对问题的一些更新,我终于做了一个wireshark捕获所有流量,我刚刚发现Game Center需要使用STUN来克服Router/NAT/Firewalls,并通过偷看在 RFC 上,它说“STUN 在用于获取地址以与恰好位于同一 NAT 后面的对等方通信时不起作用。”,好吧,我想这可能是我的问题的根源,2我的客户都在 wifi 路由器的专用网络 (192.168.2.x) 中。但是我不确定在进行测试时克服这个问题的最佳方法是什么……有什么想法吗?
  • 我和你有同样的问题,但我认为你上面提到的这个 STUN 问题不是问题,因为我使用连接到两个不同 NAT 网络的设备进行测试。

标签: iphone objective-c game-center


【解决方案1】:

所以我遇到了这个问题,解决方案(对我来说)有点尴尬。我从 Apple 文档中复制并粘贴了一堆代码......他们遗漏了一个明显的步骤。他们从未真正设置比赛的代表!

我现在的代码是:

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match {
[self dismissModalViewControllerAnimated:YES];
self.myMatch = match; // Use a retaining property to retain the match.
self.myMatch.delegate = self;  // THIS LINE WAS MISSING IN THE APPLE DOCS.  DOH.
// Start the game using the match.
NSLog(@"Match started! Expected Player Count:%d  %@",match.expectedPlayerCount, match.playerIDs);}

一旦我真正设置了匹配委托,函数就会被调用。呵呵。

【讨论】:

    【解决方案2】:

    当您获得 GKMatch 对象时,请务必检查 expectedPlayerCount 属性。可能其他玩家已经连接,因此您不会在委托上获得 match:player:didChangeState。

    【讨论】:

    • 相同的代码在相同的 NAT 中有效,但在不同的 NAT 中无效。所以事实并非如此。
    • 你检查过房产吗?这完全是时间相关的,因此在不同的网络上会有不同的表现。
    【解决方案3】:

    我和朋友也遇到过同样的问题。解决方案很奇怪,但之后就可以了。在所有设备上,您必须在设置/通知选项中为游戏中心启用通知(声音/警报/徽章)。之后我们可以建立连接并确实收到了一个匹配对象

    【讨论】:

    • 我不知道这是否有效,因为我正在使用 ios 模拟器进行开发测试。一个在我的本机机器上运行,第二个在 os x 服务器上的 vmware 中运行。 ios 模拟器中不包含通知。
    【解决方案4】:

    在你的回调 matchmakerViewController:didFindMatch 中

    添加此代码,您将看到 GC 调用回调“match:player:didChangeState”

    GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
        request.minPlayers = 2;
        request.maxPlayers = 2;
        [[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:request completionHandler:^(NSError* error) {
            if(error)
                NSLog(@"Error adding player: %@", [error localizedDescription]);
        }];
    

    【讨论】:

      【解决方案5】:

      它一直在工作。唯一的区别是......当您使用邀​​请时,不会调用事件“didChangeState”。您已连接,恕不另行通知,您可以开始接收数据。我从来没有尝试过发送/接收数据......因为我首先期待这个事件,但我确实有一次错误地发送了一些东西,并且它起作用了。 :)

      - (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *) match {    
          //Dismiss window
          [self dismissModalViewControllerAnimated:YES];
      
          //Retain match
          self.myMatch = match; 
      
          //Delegate
          myMatch.delegate = self;
      
      
          //Flag
          matchStarted = TRUE;
      
         //Other stuff
      }
      
      - (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state  {
          //This code gets called only on auto-match
      }
      

      上面的代码按预期工作。

      【讨论】:

        【解决方案6】:

        确保您已将您的班级设置为GKSession 的代表。该类将需要实现GKSessionDelegate 协议...否则,它将永远不会收到此回调。这是protocol reference。希望这会有所帮助!

        【讨论】:

        • 谢谢山姆,但我实际上是使用匹配而不是点对点来实现多人游戏。所以我已经实现并正确设置了 GKMatchDelegate 协议和 GKMatchmakerViewControllerDelegate,但似乎无法接收到 match:player:didChangeState 回调。
        猜你喜欢
        • 1970-01-01
        • 2013-06-03
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-01
        相关资源
        最近更新 更多