我试图让我的游戏允许1个设备(iPhone)使用GameCenter邀请朋友来玩(iPad)。我正在使用standarddefault MatchMaker界面。iPhone向iPad发送邀请,iPad会出现一个通知。
当我按下这个通知时,iPad的 "player(GKPlayer, didAccept: GKInvite) "例程确实被调用。
@objc func player(_ playerMe: GKPlayer, didAccept invite: GKInvite) {
print("\n\n\t\tplayer \(playerMe.displayName)(\(playerMe.playerID)) did accept INVITE sent by \(invite.sender.displayName)(\(invite.sender.playerID))")
GKMatchmaker.shared().match(for: invite, completionHandler: {(InvitedMatch, error) in
print("\t\tplayers.count = \(InvitedMatch!.players.count)")
if error != nil {
print("INVITE ERROR: \(error.debugDescription)")
}
if InvitedMatch != nil {
print("\t\tSetting current match. (\(InvitedMatch.debugDescription))")
self.currentMatch = InvitedMatch
self.currentMatch?.delegate = self
// self.prepareMatch()
}
})
}
输出。
player Me(G:25139341913) did accept INVITE sent by “-----”(G:12453976)
players.count = 0
Setting current match. (Optional(<GKMatch 0x282d39970 expected count: 1 seqnum: 0
G:12453976:unknown
reinvitedPlayers:(
)>))
玩家数组是空的!至少应该有邀请者在里面吧?'expectedPlayerCount'正确地反映了2个人的matchRequest,其中1个玩家(邀请者)已经是参与者了。
任何时候,两端都没有调用'player(GKPlayer,didRequestMatchWithRecipients:[GKPlayer])'。
所以,iPad没有进入球员设置比赛的权限,但iPhone看到邀请被接受,有2个球员,就继续前进。iPhone的代码。
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
print("\n\n\t\tMATCH FOUND\n\n")
viewController.dismiss(animated: true, completion: nil)
GKMatchmaker.shared().stopBrowsingForNearbyPlayers()
currentMatch = match
match.delegate = self
if Globals.gameState?.currentState is StateWaitingForMatch {
if currentMatch?.expectedPlayerCount == 0 {
prepareMatch()
}
}
}
那我怎么才能让iPad(邀请函的接收方)看到包括球员?
在player(GKPlayer, invite: GKInvite)方法中,通过GKMatchMakerViewController创建一个GKMatchMakerViewController。
让mmvc = GKMatchmakerViewController(invite: invite!)
然后呈现它。
viewController.present(mmvc!, animated: true, completion: nil)