我正在尝试使用媒人制作自定义配对视图。以下代码用于查找匹配项。
当我在具有不同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!");
}
}];
答案 0 :(得分: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.
}
}