0

我正在尝试使用媒人制作自定义媒人视图。下面的代码用于查找匹配项。

当我在具有不同 Game Center 帐户的两台不同设备上运行此程序时,两者都会获得比赛,但没有人会连接到比赛。他们只会陷入无限的while循环中,永远不会出来。我错过了什么,你需要打电话来真正连接到比赛吗?

- (void) findMatch{
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = nil;
NSLog(@"Start searching!");

[matchmaker findMatchForRequest:request
               withCompletionHandler:^(GKMatch *match, NSError *error)
 {
     if (error) {
         // Print the error
         NSLog(@"%@", error.localizedDescription);
     }
     else if (match != nil)
     {
         curMatch = match;
         curMatch.delegate = self;


         NSLog(@"Expected: %i", match.expectedPlayerCount);

         while (match.expectedPlayerCount != 0){
             NSLog(@"PLayers: %i", curMatch.playerIDs.count);
         }
         NSLog(@"Start match!");
     }
 }];
4

1 回答 1

1

您不应该使用 while 循环来等待 expectedPlayerCount 达到 0,而是实现 GKMatchDelegate 方法:

- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state {
    if (!self.matchStarted && match.expectedPlayerCount == 0) {
        self.matchStarted = YES;
        //Now you should start your match.
    }
}
于 2013-05-14T20:08:46.783 回答