我正在尝试使用媒人制作自定义媒人视图。以下代码用于查找匹配项。
当我在具有不同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!");
}
}];
最佳答案
您不应该使用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 Matchmaker无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16547625/