0

我创建了一个会话,将其分享给另一个玩家,现在我想开始游戏。我可以在两台设备上看到两个玩家的会话。所以,看起来我们已经准备好进行游戏了,但我需要在游戏前更改连接状态,我可以在两台设备上执行此操作。

但是......当我在设备 A 上执行此操作时,我看到用户 A 已连接,而用户 B 未连接。之后,当我在设备 B 上重复该过程时,我看到反之亦然的情况。B 已连接,A 未连接。

这是我连接播放器并发送数据的代码:

session.setConnectionState(.connected) { (error) in
   if let err = error {
      assertionFailure(err.localizedDescription)
   }
   else {

      print("NC:",session.players(with: .notConnected))
      print(" C:",session.players(with: .connected))
            
      let m = MoveTransfer(move:1) // test struct to send/receive data
        
      session.send(m.data(), with: .reliable) { (error) in
         if let err = error {
            assertionFailure(err.localizedDescription)
         }
      }
   }
}

我收到错误:

The requested operation could not be completed because there are no recipients connected to the session

顺便说一句,我无法更改模拟器上的连接状态(iCloud 已登录)。

我忘了说,我正在开发一款回合制游戏。

编辑

再次尝试,现在经过几次迭代,我得到了这个:

我让两个玩家都连接到会话。但是发送数据仍然不起作用。

这是控制台输出:

NC: [] // not connected array and connected array below
C: [<GKCloudPlayer: 0x17402e700>, id: playerID1, name: Player1,<GKCloudPlayer: 0x17402e900>, id: playerID2, name: Player2]

fatal error: The requested operation could not be completed because there are no recipients connected to the session.

在两个真实设备上得到了这个。

4

1 回答 1

1

我能够在另一台设备上发送和接收数据。我使用 loadSessions 函数来加载所有会话(我认为 loadSession by id 也可以解决问题)。我做了以下操作,D1 是设备 1,D2 是设备 2:

1. D1: load all sessions and set player state to connected
2. D2: load all sessions and set player state to connected
3. D1: load all sessions again and set player state to connected
4. D1 || D2: send data
5. D2 || D1: data 256 bytes from player: <GKCloudPlayer: 0x1c022b380>, id: playerID, name: (null)

虽然,我无法在两个设备上来回传输数据,因为如果我们添加第 6 步,即从刚刚接收到数据的设备发送数据,我们将收到错误消息:The requested operation could not be completed because there are no recipients connected to the session.

我不知道是什么原因,但我在这个阶段停止了我的努力,因为我现在认为,我不需要连接到会话来玩回合制游戏。

我发现这个“新”的 iOS 10 API 被遗弃了。由苹果和开发人员因此。如果您是仍在尝试使用它的人之一,如果您想讨论 GKGameSession 和相关主题,请在我的推特上给我留言(链接在我的简历中)。

于 2017-07-06T10:56:29.707 回答