2011-01-25 66 views
5

我试图使用游戏中心后称为:多玩家游戏中心:匹配委托的未找到匹配

截至目前,玩家要进行身份验证游戏中心,它们可以发送/读取的分数,并acheivements。 对于多人游戏功能,我尝试了两种方法: - 使用游戏中心界面来查找比赛。 - 以编程方式查找匹配项。

对于这两种方式,我有以下问题:匹配委托的匹配:player:didChangeState:方法不被调用。 在苹果文档中,声明如果一个玩家连接或断开连接,则会调用此代理。

在我的情况下,这个委托永远不会被调用。我认为我错过了一步。 这里是在执行我的委托之后(如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. 
    } 
} 

并且还找到匹配的代码。

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

感谢您的帮助。

回答

1

它一直在工作。唯一的区别是...当您使用邀​​请事件“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 
} 

上述代码按预期工作。

+0

我不相信这是真的。我刚刚测试过自动匹配,didChangeState从不会触发。只有didFindMatch。我认为didChangeState可能只有在玩家进入GKPlayerStateUnknown然后回来,或者他们被添加到进行中的比赛时才会发生。 – 2014-12-02 09:23:51

0

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