问题描述
我正在尝试使用SpriteKit在GameCenter中实现实时比赛。
这是我的场景:
class IntroScene: SKScene,GameCenterManagerDelegate {
var gameCenterManager: GameCenterManager?
override func didMove(to view: SKView) {
GameCenterManager.manager.delegate = self
<other code>
}
override func didChangeSize(_ oldSize: CGSize) {
super.didChangeSize(oldSize)
// Doing it here since authentication takes place after setting up UI
// that is changing based on the layout
GameCenterManager.manager.authenticatePlayer()
}
func didChangeAuthStatus(isAuthenticated: Bool) {
print("....INTROSCENE: DELEGATE DID_CHANGE_AUTH_STATUS CALLED......",isAuthenticated)
// ...Enabling the Button to play in GameCenter
}
func presentGameCenterauth(viewController: UIViewController?) {
guard let vc = viewController else {return}
self.view!.window?.rootViewController?.present(vc,animated: true)
}
// Presenting the matchmaker
func presentMatchmaking(viewController: UIViewController?) {
guard let vc = viewController else {return}
self.view!.window?.rootViewController?.present(vc,animated: true)
}
// Problem here. Never gets called
func presentGame(match: GKMatch) {
print("....INTROSCENE: DELEGATE PRESENT_GAME CALLED......")
// ... Presenting a new scene
}
}
这是我GameCenterManager的一部分:
final class GameCenterManager : NSObject,GKLocalPlayerListener,gkmatchmakerViewControllerDelegate {
static let manager = GameCenterManager()
weak var delegate: GameCenterManagerDelegate?
var match: GKMatch?
func authenticatePlayer() {
GKLocalPlayer.local.authenticateHandler = { gcAuthVC,error in
self.delegate?.didChangeAuthStatus(isAuthenticated: GKLocalPlayer.local.isAuthenticated)
if GKLocalPlayer.local.isAuthenticated {
GKLocalPlayer.local.register(self)
}
else if let vc = gcAuthVC {
self.delegate?.presentGameCenterauth(viewController: vc)
}
else {
print(">>>>> Error authenticating the Player! \(error?.localizedDescription ?? "none") <<<<<")
}
}
func presentMatchmaker() {
guard GKLocalPlayer.local.isAuthenticated else { return }
let request = GKMatchRequest()
request.minPlayers = minPlayers
request.maxPlayers = maxPlayers
request.inviteMessage = "Would you like to play?"
guard let vc = gkmatchmakerViewController(matchRequest: request) else { return }
vc.matchmakerDelegate = self
delegate?.presentMatchmaking(viewController: vc)
}
// This seems to be working !!!
func player(_ player: GKPlayer,didAccept invite: GKInvite) {
print("-----player -- did accept invite-------\(player.displayName)")
}
// ... AND THIS IS NOT WORKING!!!
func matchmakerViewController(_ viewController: gkmatchmakerViewController,didFind match: GKMatch) {
print("-----matchmakerVC did find match-------")
viewController.dismiss(animated: true)
match.delegate = self
delegate?.presentGame(match: match)
}
}
因此,基本上就是这样。玩家1开始游戏。播放器成功通过身份验证。我邀请玩家2加入游戏。邀请被发送到播放器2的设备。玩家2接受游戏(打印玩家确实接受了邀请)。但是,此后没有任何反应。玩家1的设备上不断旋转的轮子上说“正在发送”,并且游戏场景从未启动。我从没收到有关找到匹配项的通知,即从未调用matchmakerViewController(_ viewController:,match:)
。
有人有类似的问题吗?这是我第一次使用GameKit,这非常令人沮丧。
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)