我已经实现了一个自定义媒人,作为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);
}
}
}];但是,在playerID更改为GKPlayerStateConnected之前,我无法通过以下方式获得它们:
- (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:(
)>匹配的playerID数组(第二个NSLog())在创建匹配后立即为空,这是有意义的,因为尚未正式建立连接:
player IDs: (
)我终于回答了这些问题(谢谢你的耐心):
1a)处于未知状态的玩家的ID来自哪里?
很明显,它在火柴里,但它究竟存放在哪里?我只看到与playerID相关的数组,它是空的。
( 2)获得playerID还有其他(合法)途径吗?也就是说,在它们改变到连接状态之前
发布于 2013-04-14 23:18:35
在您的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:检索玩家的别名etc
发布于 2013-03-13 14:22:09
好吧,回答我自己的问题。可以这样做:
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
}
}https://stackoverflow.com/questions/15194132
复制相似问题