游戏中心以编程方式查找匹配项

时间:2010-10-07 20:02:19

标签: iphone center match

我无法弄清楚它是如何工作的。我想要做的是让两个玩家玩游戏,如果第三个玩家加入它可以立即加入游戏,如果第四个和最后一个玩家加入它也可以立即加入游戏。他们也可以随时出于任何原因离开游戏,如果发生这种情况,应该为另一个人或同一个人重新连接打开一个空间。这就是主意。

现在我得到的是以下内容。我出于显而易见的原因验证了本地播放器。然后我像这样搜索一个匹配:

if (matchRequest) [matchRequest release];
matchRequest            = [[GKMatchRequest alloc] init];
matchRequest.minPlayers = 2;
matchRequest.maxPlayers = 4;

[[GKMatchmaker sharedMatchmaker] findMatchForRequest:matchRequest withCompletionHandler:^(GKMatch *match, NSError *error) {
    if (error) {
        // An error occured
    } else {
        if (matchCurrent) [matchCurrent release];
        matchCurrent          = [match retain];
        matchCurrent.delegate = self;
    }
}];

如果我在三个不同的设备上执行此部分,其中两个将找到对方,第三个仍然在寻找。所以我想在找到匹配请求之后找到了它将被执行一次的最小数量的玩家。所以我需要的是一种方法,它使用我保留的matchCurrent来添加更多玩家。 Luckely这种方法存在,但是如何运作呢?在这种情况下你什么时候打电话?我决定将它放在一个按钮下,这样我就可以在找到匹配项时手动执行它。

我发现当我在第一个设备上按下它时,最后第三个设备可以找到第一个和第二个设备所在的匹配。事实上,第二个和第三个设备包含所涉及的每个设备的playerID。这是件好事。但是有两个问题。

  1. 哪个设备实际上应该调用addPlayersToMatch方法?如何将它限制为执行该方法的一个设备?加上什么时候打电话给它?
  2. 为什么在调用该方法的设备上,不是更新了playerID?

    [[GKMatchmaker sharedMatchmaker] addPlayersToMatch:matchCurrent matchRequest:matchRequest completionHandler:^(NSError *error) {
        //matchCurrent.playerIDs is not updated?!
    }]; 
    

    实际上它们已更新。当我看到第二个和第三个设备上出现播放器ID时,我手动更新设备1上的matchCurrent.playerIDs,突然它确实识别出播放器。但是,当在设备1上发现新玩家时,甚至不会调用播放器的“didChangeState”。

4 个答案:

答案 0 :(得分:2)

您使用的是Apple iOS游戏中心GKMatchmaker课程。我假设您使用的是点对点连接,而不是托管。

GKMatch类为您提供了playerIDs数组。

@property(nonatomic, readonly) NSArray *playerIDs

这是一个有序列表,因此您可以使用它来选择第一个玩家调用addPlayersToMatch。

以下链接是一些文档。

http://developer.apple.com/library/ios/#documentation/GameKit/Reference/GKMatchmaker_Ref/Reference/Reference.html

http://developer.apple.com/library/ios/#documentation/GameKit/Reference/GKMatch_Ref/Reference/Reference.html

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008304

答案 1 :(得分:1)

  

实际上应该调用哪个设备   addPlayersToMatch方法?怎么可以   您将其限制为一个设备   执行该方法?

您可以通过让设备“吸管”来解决这个问题。每个设备生成一个随机数,然后将其发送给其他设备。具有最大编号的设备是领导者,并且它必须调用addPlayersToMatch。如果两个设备选择相同的号码,请丢弃这些号码并重新开始。

答案 2 :(得分:0)

我建议您定期(可能每秒或每两次一次)对其他玩家的状态进行投票,以便您可以检测是否有人因任何原因加入或离开。除非您使用的iphone架构已经提供了处理该事件的功能。

听起来您可能想要为您正在使用的多人游戏框架找到更多文档和/或示例代码。

答案 3 :(得分:0)

我通过 Prime31's GameCenterMultiplayerBinding 使用 Unity 执行此操作。我不完全确定它是如何映射到 GameKit 的(文档很少,没有提供这些细节),但名称非常具有启发性。

为了能够将 2 到 4 名玩家匹配到一场比赛中,我正在做:

findMatchProgrammaticallyWithFilters(
                        minPlayers: 2,
                        maxPlayers: 4,
                        playerGroup: GetCurrentPlayerGroup(),
                        playerAttributes: 0);

我假设这映射到 findMatchForRequest:withCompletionHandler

成功后,我调用:

finishMatchmakingForMatch();

这肯定映射到 finishMatchmakingForMatch。我发现如果我没有先调用 finishMatchmakingForMatch,调用 findMatchForRequest 或 addPlayersToMatch 会失败并显示“请求的操作已被用户取消或禁用”。

addPlayersToCurrentMatchWithUpdatedMatchRequest(
                        minPlayers: 2,
                        maxPlayers: 4,
                        playerGroup: GetCurrentPlayerGroup(),
                        playerAttributes: 0,
                        playersToInvite: null);

addPlayersToMatch:matchRequest:completionHandler

成功后,我调用:

finishMatchmakingForMatch();

如果我有更多玩家的空间,我会循环并再次调用 addPlayersToMatch

我希望这样做,因为我需要在完成后调用 finishMatchmakingForMatch 匹配,发射 findMatchProgrammatically 会一直寻找直到它 找到了 maxPlayers,但没有。它在第一场比赛后放弃。所以我们需要 呼叫 addPlayersToMatch

相关问题