iphone - iOS GameCenter 匹配器不工作

标签 iphone ios objective-c xcode game-center

我正在尝试使用匹配器创建自定义匹配 View 。下面的代码用于查找匹配项。

当我在具有不同游戏中心帐户的两个不同设备上运行此程序时,两者都会获得匹配,但没有一个会连接到匹配。他们只会陷入无限的 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!");
     }
 }];

最佳答案

您不应该使用 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.
    }
}

关于iphone - iOS GameCenter 匹配器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16547625/

相关文章:

ios - 如何知道 UIButton titlerable.text 是否改变

ios - 当persistentContainer不在Swift的App委托(delegate)方法中时如何从核心数据中删除所有数据

ios - 在 Parse 中从对应于 Color 的索引路径设置 Cell Background Color

iphone - 如何在 iPhone 应用程序中播放声音

iPhone 模拟器和网络摄像头

iPhone URL 请求 : Add value to HTTP header field

视频标签的Javascript操作

ios - 来自 UITableView 的 deleteRowsAtIndexPaths :withRowAnimation: 的不规则动画

objective-c - 当库中没有类采用它时,如何强制将协议(protocol)链接到库中?

objective-c - 调整大小后的图像未正确绘制为位图