我正在尝试使用匹配器创建自定义匹配 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/