1

我正在尝试使用 Game Center 默认功能保存比赛数据。以下功能工作得很好,事情得到了保存。

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

        print(e ?? "No Error:")

 })

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

4

1 回答 1

3

Yes, the newer way is to use the player listener to detect this kind of change in the match. I have created an example project of a turn based GameKit game that you are welcome to use as a skeleton. The key function is found in the ViewController.swift file:

///    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
            }
        }
    }
}

The references to the game model will make much more sense in the context of the whole example project, but to your question: notice the "refreshing data for this match" line? That is how you detect new incoming match data. You can do with it whatever you need.

于 2017-07-27T22:18:24.447 回答