我创建了一个会话,将它分享给了另一个玩家,现在我想开始一个游戏。我可以看到两个设备上都有两个播放器的会话。因此,看起来我们已经准备好玩游戏了,但我需要在游戏前更改连接状态,我可以在两个设备上执行此操作。
但是..。当我在设备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已登录)。
我忘了提一下,我正在开发一个基于回合的游戏。
编辑
尝试了一次又一次,经过几次迭代,我得到了以下结果:
我已经把两个玩家都连接到session了。但是发送数据仍然不起作用。
以下是控制台输出:
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.这是在两个真实的设备上得到的。
发布于 2017-07-06 18:56:30
我能够在另一台设备上发送和接收数据。我使用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 10API被抛弃了。被苹果公司和随后的开发人员使用。如果你是那些仍在尝试使用它的人中的一员,如果你想讨论GKGameSession和相关话题,请在我的推特上给我留言(我的简历中的链接)。
https://stackoverflow.com/questions/44908027
复制相似问题