我有一个游戏中心的应用。我成功地连接了两个客户端,并可以发送消息等。我现在正试图添加一个第3/4客户端的[GKMatchmaker addPlayersToMatch],像这样。
- (void) findAdditionalPlayer
{
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2; // minPlayers = 3 doesn't work either
request.maxPlayers = 4;
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:request completionHandler:^(NSError *error)
{
if (error)
{
// Process the error.
NSLog(@"Could not find additional player - %@", [error localizedDescription]);
}
else
{
NSLog(@"Find additional player expecting = %d", match.expectedPlayerCount);
}
}];
}如果一个客户机(投票服务器)调用findAdditionalPlayer,我就不会连接(另一个客户机正在使用GKMatchmakerViewController)。奇怪的是,如果两个连接的客户端都调用了findAddtionalPlayer,那么我的完成块就会执行(the match.expectedPlayerCount == 2),但是我的第三个客户端从不连接。
上面应该有一个游戏客户调用这个功能吗?文档并没有真正指定。
有人有使用addPlayersToMatch的示例吗?
发布于 2012-02-26 05:48:28
根据我的经验,对于一个2人的游戏,addPlayersToMatch应该由双方执行,以使他们重新连接到游戏(并通过游戏中心来回交流)。
如果两个客户端都调用findAdditionalPlayer,那么您的两个客户端就可以连接,因为它们都在调用addPlayersToMatch。
如果你已经和两个玩家(比如A和B)玩了一场比赛,并且你想让第三个玩家(比如C)加入,你必须:
在玩家A(邀请C)中:
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 3;
request.maxPlayers = 4;
request.playersToInvite = [NSArray arrayWithObject:playerC_id];
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:myMatch matchRequest:request completionHandler:nil];在玩家B(邀请C)中:
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 3;
request.maxPlayers = 4;
request.playersToInvite = [NSArray arrayWithObject:playerC_id];
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:myMatch matchRequest:request completionHandler:nil];在玩家C中(邀请A和B):
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 3;
request.maxPlayers = 4;
request.playersToInvite = [NSArray arrayWithObjects:playerA_id, playerB_id, nil];
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:myMatch matchRequest:request completionHandler:nil];因此,重新加入或在比赛中增加新球员的机制似乎是:
当播放机检测到远程播放机断开连接(或添加了一个全新的播放器)时,创建一个新的匹配请求对象,在此新匹配请求的数组中只包含已断开/新玩家的id,并与其一起执行addPlayersToMatch。当断开连接的播放机恢复时,创建一个新的匹配请求对象,在其匹配请求的playersToInvite数组中包含所有远程玩家的id(您可能必须预先将它们存储在数组中或从GKMatch的播放id属性中获取它们),并与之一起执行addPlayersToMatch。
换句话说,每个现有的玩家都会向他们的匹配对象中添加新的球员,而新的玩家则会将所有的现有玩家添加到它的match对象中。
对于一个4人游戏(玩家A、B、C和D,其中player D是最新添加的):玩家A、B和C各自使用匹配请求对象执行他们的addPlayersToMatch调用,其playerToInvite仅由D的playerID组成。player D使用一个match请求对象执行其addPlayersToMatch调用,该对象的playerToInvite数组包含A、B以及C的播放‘s。
https://stackoverflow.com/questions/8810691
复制相似问题