游戏中心:匹配代表在找到匹配后未被调用

sim*_*eam 5 delegates multiplayer ios game-center

我正在尝试使用游戏中心:多人游戏

到目前为止,玩家正在认证游戏中心,他们可以发送/读取分数和成就.对于多人游戏功能,我尝试了两种方法: - 使用游戏中心界面查找匹配项. - 以编程方式查找匹配项.

对于这两种方式,我有以下问题:匹配委托的匹配:player:didChangeState:方法未被调用.在apple docs中,声明如果一个玩家连接或断开连接,则会调用该委托.

在我的情况下,这个委托永远不会被调用.我想我错过了一步.在我的委托实施后(如Apple doc中所述).

- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state
{
    switch (state)
    {
        case GKPlayerStateConnected:
            // handle a new player connection.
           break;
        case GKPlayerStateDisconnected:
            // a player just disconnected.
           break;
    }
    if (!self.matchStarted && match.expectedPlayerCount == 0)
    {
        self.matchStarted = YES;
        // handle initial match negotiation.
    }
}
Run Code Online (Sandbox Code Playgroud)

以及找到匹配的代码.

-(void) findProgrammaticMatch
{
  GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
  request.minPlayers = 2;
  request.maxPlayers = 2;

  [[GKMatchmaker sharedMatchmaker] findMatchForRequest:request
                                 withCompletionHandler:^(GKMatch *FoundMatch, NSError *error)
  {
    if (error)
    {
      // Process the error.
      StatusLabel.text = @"Match Not Found";
    }
    else if (FoundMatch != nil)
    {
      MultiPlayerMatch = FoundMatch; // Use a retaining property to retain the match.
      StatusLabel.text = @"Match Found";
      MultiPlayerMatch.delegate = self; // start!
      // Start the match.
      // Start the game using the match.
      [self StartMatch];
    }
  }];
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

Rob*_*agu 2

它一直在起作用。唯一的区别是......当您使用邀​​请时,事件“didChangeState”不会被调用。您已连接,恕不另行通知,并且可以开始接收数据。我从未尝试过发送/接收数据,因为我首先期待该事件,但我确实有一次错误地发送了一些内容,并且它起作用了。

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *) match {    
    //Dismiss window
    [self dismissModalViewControllerAnimated:YES];

    //Retain match
    self.myMatch = match; 

    //Delegate
    myMatch.delegate = self;


    //Flag
    matchStarted = TRUE;

   //Other stuff
}

- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state  {
    //This code gets called only on auto-match
}
Run Code Online (Sandbox Code Playgroud)

上面的代码按预期工作。