1

我已经实现了一个自定义媒人作为 GKMatchmakerViewController 的直接替代品,仅在 iOS6+ 中运行时显示。它运行良好,但 GKMatchmakerViewController 中有一部分用户界面我似乎无法弄清楚。

当启动 2 人 (request.minPlayers = 2, request.maxPlayers = 2) 自动匹配时,GKMatchmakerViewController 能够使用在玩家更改为连接状态之前找到的玩家的显示名称和照片更新其 UI .

我使用以下代码启动自动匹配。建立连接,游戏开始,一切都很好。

[[GKMatchmaker sharedMatchmaker] findMatchForRequest:matchRequest withCompletionHandler:^(GKMatch *match, NSError *error) {
    if (error != nil) {
        // ...the error handling code...
    } else if (match != nil) {
        NSLog(@"An auto-match has been found: %@", match);

        if (match.expectedPlayerCount == 0) {
            [[GKMatchmaker sharedMatchmaker] finishMatchmakingForMatch:match];
        } else {
            NSLog(@"player IDs: %@", match.playerIDs);
        }
    }
}];

但是,在他们通过以下方式更改为 GKPlayerStateConnected 之前,我无法获取 playerID:

- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state;

找到匹配后,该玩家处于 GKPlayerStateUnknown 中。但是,代码中的匹配 NSLog() 显示了 playerID(它绝对不是 localPlayer 的 ID;真实数字已被编辑):

An auto-match has been found: <GKMatch 0x210b2c90 expected count: 1 seqnum: 0
    G:1234567890:unknown
reinvitedPlayers:(
)>

匹配的 playerIDs 数组(第二个 NSLog())在匹配创建后立即为空,这是有道理的,因为尚未正式建立连接:

player IDs: (
)

我终于回答了问题(感谢您的耐心等待):

1a) 处于未知状态的玩家的ID是从哪里来的?

1b)它显然在比赛中,但它到底存储在哪里?我看到的唯一与 playerID 相关的是空数组。

2) 是否有其他(合法)方式来获取该 playerID?即在它们变为连接状态之前

4

2 回答 2

0

好的,回答我自己的问题。可以这样做:

NSLog(@"An auto-match has been found: %@", match);

NSString * matchDescription = [match description];
NSLog(@"%@", matchDescription);   // displays the same thing as NSLog(@"%@", match)

NSRange gRange = [matchDescription rangeOfString:@"G:"];
if (gRange.location != NSNotFound) {
    NSRange endSearchRange;
    endSearchRange.location = gRange.location + 2;  // skip the G:
    endSearchRange.length = matchDescription.length - endSearchRange.location;
    NSRange endRange = [matchDescription rangeOfString:@":" options:NSLiteralSearch range:endSearchRange];

    if (endRange.location != NSNotFound) {
        NSUInteger idSpan = endRange.location - gRange.location;
        gRange.length = idSpan;
        NSString * opponentPlayerID = [matchDescription substringWithRange:gRange];
        NSLog(@"%@", opponentPlayerID);    // G:1234567890

        // update the UI with the opponent's info
    }
}
于 2013-03-13T14:22:09.560 回答
0

playerIDfindMatchForRequest完成处理程序中有:

[[GKMatchmaker sharedMatchmaker] findMatchForRequest:matchRequest withCompletionHandler:^(GKMatch *match, NSError *error) {
  // -- removed error checking code for short --
  puts( "PLAYER ID's:" ) ;
  for( NSString* ns in match )
      puts( [ ns UTF8String ] ) ; // FORMAT: G:37145177499. DO NOT FUDGE WITH THE STRING.

    [GKPlayer loadPlayersForIdentifiers:theMatch.playerIDs withCompletionHandler:^( NSArray *players, NSError *nsError ) {
      puts( "The REMOTE player aliases are:" ) ;
      if( !nsError )
        for( GKPlayer* p in players )
          puts( [p.alias UTF8String ] ) ;
   } ] ;
}];

然后,您可以从GKPlayer loadPlayersForIdentifiers:withCompletionHandler 中检索播放器的别名等:

于 2013-04-14T23:18:35.270 回答