我正在尝试学习如何构建基于游戏中心的应用程序。这是一款基于回合的极其简单的游戏,基本上只是记录您按下的按钮并将其发送给其他玩家。由于 Apple 的文档尚未针对 Swift 进行更新,因此我很难实现所有游戏中心功能。我一直在猜测一切并阅读 Objective-C 示例并希望得到最好的结果(不知何故我已经设法让一些事情顺利进行,尽管我不确定它们是否正确) 不管怎样,我为我的模拟器创建了一个新的 iCloud 帐户,并同时在我的手机和模拟器上运行该应用程序,试图让它们在游戏中匹配。但是我总是收到“匹配请求无效”错误:
编辑我已经在 iTunesConnect 中注册了该应用程序,我已经实现了排行榜并对其进行了测试,它们可以正常工作(所以我假设 iTunesConnect 功能正常工作和设置)
@IBAction func startTapped(_ sender: Any) {
let request = GKMatchRequest()
request.maxPlayers = 2
request.minPlayers = 1
let mmvc = GKMatchmakerViewController(matchRequest: request)
mmvc?.matchmakerDelegate = self
present(mmvc!, animated: true, completion: nil)
}
extension HomeVC: GKGameCenterControllerDelegate
{
func authenticatePlayer()
{
//CALLED ON VIEWDIDAPPEAR
let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {
(view, error) in
if view != nil
{
self.present(view!, animated: true, completion: nil)
} else {
print("AUTHENTICATED!")
print(GKLocalPlayer.localPlayer().isAuthenticated)
}
}
}
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismiss(animated: true, completion: nil)
}
}
这是匹配代码。请注意,我可以显示游戏中心屏幕,并告诉我“有多少玩家”以及缺少一名玩家,并可以选择邀请 friend 等等
extension HomeVC: GKMatchmakerViewControllerDelegate {
func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
print("match was cancelled")
viewController.dismiss(animated: true, completion: nil)
}
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) {
print("didFailwithError: \(error.localizedDescription)")
}
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
print("Match found, ID: \(match.description)")
let gameScreenVC = self.storyboard?.instantiateViewController(withIdentifier: "mainGame") as! ViewController
gameScreenVC.providesPresentationContextTransitionStyle = true
gameScreenVC.definesPresentationContext = true
gameScreenVC.modalPresentationStyle = UIModalPresentationStyle.fullScreen
gameScreenVC.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
match.delegate = gameScreenVC
self.present(gameScreenVC, animated: true, completion: nil)
}
}
最佳答案
如果有人遇到这个问题,我只需将“minelayers 和 max-layers”替换为相同数量即可修复它,因此代码现在如下所示:
@IBAction func startTapped(_ sender: Any) {
let request = GKMatchRequest()
request.maxPlayers = 2 //min and max should be the same
request.minPlayers = 2 //apparently they need to be the same
let mmvc = GKMatchmakerViewController(matchRequest: request)
mmvc?.matchmakerDelegate = self
present(mmvc!, animated: true, completion: nil)
}
显然 Game Center 不喜欢 min.player 计数为 1,它是无效的。但这就是我如何让它停止向我发出无效匹配请求警告
关于swift - 游戏中心匹配始终无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48379040/