6

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

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

对于这两种方式,我都有以下问题:未调用匹配委托的 match:player:didChangeState: 方法。在苹果文档中,如果一个玩家连接或断开连接,就会调用这个委托。

在我的情况下,这个代表永远不会被调用。我认为我错过了一步。在我的委托实施之后(如苹果文档中所述)。

- (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.
    }
}

以及查找匹配项的代码。

-(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];
    }
  }];
}

谢谢你的帮助。

4

2 回答 2

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
}

上面的代码按预期工作。

于 2011-06-16T13:47:58.473 回答
0

我认为 didChangeState: GKPlayerStateConnected 可能只发生在玩家是 GKPlayerStateUnknown 然后回来的情况下,或者如果他们被添加/邀请回到正在进行的比赛中。

于 2014-12-02T09:28:20.017 回答