2

在我的 iOS 回合制比赛中,我正在尝试接收通知并获取

public func player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool)

被调用,没有成功。

我将我的视图模型注册到本地播放器

 GKLocalPlayer.localPlayer().register(self)

我希望在其他玩家执行后触发

func endTurn(withNextParticipants nextParticipants: [GKTurnBasedParticipant], turnTimeout timeout: TimeInterval, match matchData: Data, completionHandler: ((Error?) -> Swift.Void)? = nil)

但没有成功。

如果我强制重新加载 matchData,那么我将获得第二个玩家刚刚提交的数据。所以 endTurn 工作正常。

有什么我做错了吗?

更新:所以我创建了一个新项目,复制了我所有的文件,仅启用了 Game Center 功能。

当开发它运行完美时,我连接了两个设备(具有不同的苹果 ID)。通知正在工作,Turnbasedlistener 正在触发。

我一发布它进行内部测试,它就停止工作了!!!

4

2 回答 2

1

我有非常相似的问题。我的解决方案是在等待轮到我时手动重新检查我的状态。首先,我定义了全局变量 var gcBugTimer: Timer

endTurn(withNextParticipants:turnTimeOut:match:completionHan‌​dler:)完成处理程序中:

let interval = 5.0
self.gcBugTimer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(self.isMatchActive), userInfo: nil, repeats: true)
self.gcBugTimer.tolerance = 1.0

上面的代码也应该被调用,以防玩家对新的比赛和其他玩家感到高兴。

然后定时器方法:

func isMatchActive() {
  // currentMatch - global variable contains information about current match 
  GKTurnBasedMatch.load(withID: currentMatch.matchID!) { (match, error) in
    if match != nil {
      let participant = match?.currentParticipant
      let localPlayer = GKLocalPlayer.localPlayer()
      if localPlayer.playerID == participant?.player?.playerID {
        self.player(localPlayer, receivedTurnEventFor: match!, didBecomeActive: false)
      }
    } else {
      print(error?.localizedDescription ?? "")
    }
  }
}

我在开头添加了以下代码player(_:receivedTurnEventFor:didBecomeActive)

if gcBugTimer != nil && gcBugTimer.isValid {
  gcBugTimer.invalidate()
}
于 2017-06-30T19:56:30.593 回答
1

最终对我有用的是在实际设备上进行测试,而不是在模拟器中进行测试。receivedTurnEvents 函数似乎在模拟器中不起作用。

Grigory 的解决方法非常适合使用模拟器进行测试。

于 2018-07-09T17:29:47.523 回答