我一直在努力实现自己的GKMatchmaker接口(没有GKMatchmakerViewController)。我有一个单独的GameKitHelper类,在Ray Wenderlich教程中编写,它实现了GKMatchDelegate和GKLocalPlayerListener。当我使用GKMatchmakerViewController时,我邀请和随机配对工作完美无瑕。但是,我尝试切换到我自己的视图控制器,只需单击一个按钮就可以触发" findMatchWithminPlayers(....)"如下所示创建了一个共享的GKMatchmaker来执行" findMatchForRequest(... withCompletionHandler:...)"。我有几个println()s测试某些值,但每次match.expectedPlayerCount永远不会达到0,只是保持在1.这意味着"如果matchStarted == false和match.expectedPlayersCount == 0"永远不会满足,并且readPlayers()方法永远不会被调用/匹配无法启动。这是通过单击按钮调用的第一个方法:
func findMatchWithDetails(minPlayers: Int, maxPlayers: Int, playerGroup: Int, delegate: GameKitHelperDelegate) {
if enableGameCenter == false {
println("dont want to be in here")
return
}
matchStarted = false
gkMatch = nil
gkDelegate = delegate
var request = GKMatchRequest()
request.minPlayers = 2
request.maxPlayers = 2
request.defaultNumberOfPlayers = 2
request.playerGroup = 0
println(request)
let matchMaker = GKMatchmaker.sharedMatchmaker()
println(matchMaker)
matchMaker.findMatchForRequest(request, withCompletionHandler: { (match: GKMatch!, error: NSError!) -> Void in
if error != nil {
println("matchmaking canceled")
return
}
self.gkMatch = match
self.gkMatch?.delegate = self
println(match.expectedPlayerCount)
println(match)
if self.matchStarted == false && match.expectedPlayerCount == 0 {
println("in here")
self.lookupPlayers()
matchMaker.finishMatchmakingForMatch(self.gkMatch!)
}
})
}
这是GKMatchDelegate的玩家状态更改方法,应该在玩家连接时调用:
func match(match: GKMatch, playerID: String, didChangeState state: GKPlayerConnectionState) {
if gkMatch != match {
println("definitely dont want to be in here")
return
}
println("there was a state change")
switch(state) {
case GKPlayerConnectionState.StateConnected:
if matchStarted == false && match.expectedPlayerCount == 0 {
println("in connected")
self.lookupPlayers()
}
break
case GKPlayerConnectionState.StateDisconnected:
println("maybe in disconnected")
matchStarted = false
gkDelegate?.matchEnded()
break
default:
break
}
}
如果您能找到问题,请告诉我,我将不胜感激。我目前正在玩的两种可能性:我需要在某个地方创建一个匹配,只有一个匹配,其他设备使用几种不同的方法加入该匹配...或者我需要实现addPlayersToMatch(... )和某些地方的完成制作(...)方法。提前谢谢!