GKTurnBasedMatch:无法获取在saveCurrentTurn上调用的GKLocalPlayerListener委托方法(withMatch ..)?

时间:2017-07-05 05:58:22

标签: ios push-notification game-center gamekit gkturnbasedmatch

我正在尝试使用Game Center默认功能保存匹配数据。以下功能非常好,并且保存了一些东西。

self.myMatch?.saveCurrentTurn(withMatch: dataToSend, completionHandler: { (e) in

        print(e ?? "No Error:")

 })

这种方法是在IOS 6中引入的,当时他们像endTurnWithNextParticipant一样向对手发送推送通知,但现在在当前的IOS 10中,如果他们已经删除了发送推送通知的功能,但是应该有任何其他方法来检测对手方的matchData更新。

1 个答案:

答案 0 :(得分:0)

是的,更新的方法是使用播放器监听器来检测匹配中的这种变化。我创建了一个example project of a turn based GameKit game,欢迎您将其用作骨架。关键功能可在ViewController.swift文件中找到:

///    Activates the player's turn.
open func player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool) {
    print("***** player received turn event \(didBecomeActive ? "and became active for" : "for") match \(match.shortID)")
    dismiss(animated: true, completion: nil)

    if didBecomeActive {
        // This event activated the application. This means that the user
        // tapped on the notification banner and wants to see or play this
        // match now.
        print("***** attaching the model to this match")
        model?.match = match
    } else if model?.match?.matchID == match.matchID {
        // This is the match the user is currently playing,
        // laoding the game model below iwll update to show the latest state
        print("***** refreshing data for this match")
    } else if match.currentParticipant?.player?.playerID == model?.localPlayerID {
        // It became the player's turn in a different match,
        // prompt the player to switch to the new match
        print("***** ignoring player's turn in match \(match.shortID)")
        gameTextView.text.append("\n\nFYI, it is your turn in another match.")
        return
    } else {
        // Something has changed in another match,
        // but not sure what.
        print("***** ignoring new data for match \(match.shortID)")
        gameTextView.text.append("\n\nFYI, something has changed in another match.")
        return
    }

    print("***** loading match")
    GameModel.loadGameModel(match: match) { model, error in
        guard self.isNotError(error, during: "model loading") else { return }
        guard let model = model else {
            print("***** no model created")
            self.gameLabel.text = "Error setting up game"
            self.thereHasBeenAnError = true
            return
        }
        print("***** match load succeeded")
        self.model = model
        self.model?.checkForWin() { won, error in
            guard self.isNotError(error, during: "end match request") else { return }
            if won {
                self.gameLabel.text = "You won!"
                self.turnButton.isEnabled = false
                self.resignButton.isEnabled = false
            }
        }
    }
}

对游戏model的引用在整个示例项目的上下文中会更有意义,但对于您的问题:请注意"刷新此匹配的数据"线?这就是您检测新传入匹配数据的方式。无论你需要什么,都可以随心所欲。