Game Center回合制配对界面允许用户与多个玩家创建匹配,并通过邀请填充其中一些地方给朋友,让其他人进行自动匹配。
我正在创建我们自己的游戏内匹配创建界面,到目前为止工作正常,除非是与受邀和自动匹配玩家创建匹配。
GKMatchmaker有addPlayersToMatch方法,我相信一旦匹配存在,你可以添加自动匹配的玩家,但GKTurnBasedMatch没有等效方法。
以下是我正在使用的代码,它运行得很好。如果有人知道如何添加一些自动匹配球员,将不胜感激!
- (GKMatchRequest *) buildMatchRequestWithFriends: (NSArray *) friendsList NumberOfPlayers: (NSInteger) numberOfPlayers
{
NSLog(@"TurnByTurnHelper.buildMatchRequestWithFriends");
GKMatchRequest *request = [[GKMatchRequest alloc] init];
NSArray *playersToInvite = [NSArray arrayWithArray:friendsList];
request.playersToInvite = playersToInvite;
request.defaultNumberOfPlayers = numberOfPlayers + 1;
return request;
}
- (void) requestMatchWithFriends:(NSArray *) friendsList NumberOfPlayers: (NSInteger) numberOfPlayers{
if (!_delegate)
{
NSLog(@"Error: Expected but did not find delegate");
return;
}
GKMatchRequest *request = [self buildMatchRequestWithFriends:friendsList NumberOfPlayers: numberOfPlayers];
[GKTurnBasedMatch findMatchForRequest: request withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error)
{
if (match){
NSLog(@"findMatchForRequest: Success!");
// Add match to matches
} else {
NSLog(@"error: %@", error);
}
}];
}
答案 0 :(得分:0)
嗯,通过一些搜索和测试,我发现了Apple 开发人员文档不完整和/或误导。以下语句在GKMatchRequest的playersToInvite属性部分中进行:
“如果属性的值为非零,当您使用请求创建匹配时,Game Center会邀请这些玩家进行匹配。不会进行自动匹配,并且会忽略GKMatchRequest maxPlayers和minPlayers属性。”< / p>
对于GKMatchmaker来说这是真的,但对于GKTurnBasedMatch则不行!对于GKTurnBasedMatch,如果在playersToInvite属性中提供了一个playerId数组,则不会忽略maxPlayers和minPlayers属性,并且Game Center会使用随机播放器填充额外的空格。
以下代码可以解决问题:
- (GKMatchRequest *) buildMatchRequestWithFriends: (NSArray *) friendsList NumberOfPlayers: (NSInteger) numberOfPlayers
{
GKMatchRequest *request = [[GKMatchRequest alloc] init];
NSArray *playersToInvite = [NSArray arrayWithArray:friendsList];
if([friendsList count]<=0){
request.minPlayers = 2;
request.maxPlayers = numberOfPlayers;
request.playersToInvite = nil;
} else {
request.minPlayers = 2;
request.maxPlayers = numberOfPlayers;
request.playersToInvite = playersToInvite;
}
return request;
}
这个故事的寓意是:有时最好不要RTFM!