我正在尝试学习如何构建基于游戏中心的应用。它是一个基于转弯的非常简单的游戏,它基本上记录了你按下的按钮并将其发送给其他玩家。由于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)
}
}
这是配对代码。注意我可以出现Game Center屏幕并告诉我“有多少玩家”以及它缺少了一个玩家并且可以选择邀请朋友和所有那些
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)
}
}
答案 0 :(得分:0)
如果有人遇到这个问题,我只需用相同的金额替换“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,其无效。但这就是我如何让它停止给我无效的匹配请求警告