不调用GK​​LocalPlayerListener协议

Max*_*r89 6 gamekit ios game-center swift

正如苹果指南所说; 我已经GKLocalPlayerListener在我的游戏中心课程中实现了协议,并在他验证后立即将本地播放器添加为侦听器:

func authenticationChanged() {
  if (GKLocalPlayer.localPlayer().authenticated && !self.userAutenticated) {
    println("Authentication changed: player authenticated.")
    userAutenticated = true
    GKLocalPlayer.localPlayer().unregisterAllListeners()
    GKLocalPlayer.localPlayer().registerListener(self)
  } else if (GKLocalPlayer.localPlayer().authenticated && self.userAutenticated) {
    println("Authentication changed: player not authenticated.")
    userAutenticated = false
  }
}
Run Code Online (Sandbox Code Playgroud)

协议实施:

// MARK: - GKLocalPlayerListener
func player(player: GKPlayer!, didAcceptInvite invite: GKInvite!) {
  println("Did accept invite")
}
func player(player: GKPlayer!, didRequestMatchWithRecipients recipientPlayers: [AnyObject]!) {
  println("Did request matchmaking")
}
Run Code Online (Sandbox Code Playgroud)

当我尝试邀请朋友时,这两种方法都没有被调用,我也没有收到任何类型的通知.我试图在发布模式下测试游戏,但我得到了相同的结果.我必须说正常的配对工作正常,我能够找到没有任何问题的玩家.

编辑:

如果我从2个设备测试它,将收到通知,但如果我点击通知,该应用程序将打开,不会调用任何代表.如果我关闭应用程序并再次重新启动它,则会GKLocalPlayerListener调用它.

怎么了??

Tim*_*Tim 1

我假设,当您说“正常的匹配有效”时,您已经提供了一个 matchmakerviewcontroller:

-(IBAction)setupMatch:(id)sender{
GKMatchmakerViewController *matchViewController = [[GKMatchmakerViewController alloc] initWithMatchRequest:matchRequest];
matchViewController.matchmakerDelegate = self;
[self presentViewController:matchViewController animated:YES completion:nil];}
Run Code Online (Sandbox Code Playgroud)

然后当在 matchmakerviewcontroller 中找到玩家时 didFindMatch 将被调用:

-(void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match{
//Called when GameCenter completes the matchmaking process
match.delegate = (id)self;   //etc. lots of your own code here.
Run Code Online (Sandbox Code Playgroud)

didAcceptinvite 仅在接受邀请后在邀请接收者的设备上调用:

-(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite{
//Called on the accepting device when the invitation is accepted
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:invite];
mmvc.matchmakerDelegate = self;
[self presentViewController:mmvc animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

}

这会向您的朋友呈现一个 matchmakerviewcontroller。他们无事可做,VC 建立连接,然后自行关闭。发送者设备上的 vc 同时关闭。

然后在两个设备上调用 didFindMatch 就可以了。

我不相信 didrequestMatchWithRecipients 被调用过,并且当 didFindMatch 和 didAcceptInvite 处理两端的游戏开始时,它似乎是多余的。

我发现 WWDC 2012 的这段视频非常有帮助:WWDC 2012 Christy Warren