我已经实现了一个自定义匹配器作为GKMatchmakerViewController的直接替代品,仅在iOS6 +中运行时显示。它工作得很好,但是GKMatchmakerViewController中有一部分UI我似乎无法弄明白。
当启动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之前,我无法获取播放器ID:
- (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)是否有其他(合法)方式获得该玩家ID?即在他们改变为连通状态之前
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
您的playerID
完成处理程序中有findMatchForRequest
:
[[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:
中检索播放器的别名等