【问题标题】:Game Center's Auto-match and endTurnWithNextParticipantsGame Center 的自动匹配和 endTurnWithNextParticipants
【发布时间】:2015-02-09 08:05:02
【问题描述】:

我正在开发一款包含两名 Game Center 玩家的回合制游戏,我希望允许自动匹配。

我了解到,要真正向玩家发送邀请,邀请玩家必须结束他/她的回合。这意味着调用这个方法:

- (void)endTurnWithNextParticipants:(NSArray *)nextParticipants turnTimeout:(NSTimeInterval)timeout matchData:(NSData *)matchData completionHandler:(void (^)(NSError *error))completionHandler

现在,我不明白“nextParticipants”数组的含义,以防比赛以自动匹配模式开始,据我所知,这是通过将参与者设置为 nil 来完成的,例如:

 GKMatchRequest *request = [[GKMatchRequest alloc] init];
 request.minPlayers = 2;
 request.maxPlayers = 2;
 request.playersToInvite = nil;
 request.inviteMessage = @"Let’s play";
 [GKTurnBasedMatch findMatchForRequest: request
                 withCompletionHandler: ^(GKTurnBasedMatch *match,
                                          NSError *error) {
                     NSLog(@"%@", match);
                 }];

如果数组为nil,我不知道谁会加入比赛,我怎么可能把回合传给下一个玩家?如果我在 nextParticipants 参数中使用 nil,我当然会得到一个“无效的 nextParticipants 列表”错误。

Apple 的文档似乎对此保持沉默。

所以,我也不明白自动匹配的实际工作原理。是否会无条件地匹配任何两个已经开始新比赛的玩家自动匹配?我不能以某种方式选择我想要自动匹配的匹配类型吗? (假设,例如,游戏允许多个难度级别,而我不想自动匹配较低级别的玩家)。

编辑(根据 xcodegirl 的评论):

为了解决最后一点,只需在请求的 playerGroup 属性中添加对所需匹配类型进行编码的内容来扩展上述代码即可:

request.playerGroup = [Utils myEncodingAsNSUIntegerOfGameTypeGivenSomeParameters:...];

不过,坏事是 playerGroup 似乎不是 GKTurnBasedMatch 的可用属性。因此,如果您要列出您的比赛,包括未决的自动比赛,并希望显示有关您想玩的游戏类型的信息,您应该以其他方式存储此信息。

【问题讨论】:

    标签: ios game-center


    【解决方案1】:

    经过一番尝试,似乎本题第一部分的答案如下。 一旦比赛开始,即使没有人匹配自动匹配邀请,参与者数组也会填充所需数量的玩家(其中一个是邀请玩家),并且每个缺失的玩家都是一个 GKTurnBasedParticipant,其状态为GKTurnBasedParticipantStatusMatching。 因此,邀请玩家无需等待被邀请(自动匹配)玩家接受,就可以玩第一回合,只需创建一个下一个参与者数组,邀请玩家被放置在数组的末尾。

    NSMutableArray *nextParticipants = [NSMutableArray new];
    for (GKTurnBasedParticipant *participant in match.participants) {
        if ([participant.playerID isEqualToString:[GKLocalPlayer localPlayer].playerID]) {
            [nextParticipants addObject:participant];
        } else {
            [nextParticipants insertObject:participant atIndex:0];
        }
    }
    
    NSData *matchData = [@"some data" dataUsingEncoding:NSUTF8StringEncoding];
    
    // Send new game state to Game Center & pass turn to next participant
    [self.invitation.match endTurnWithNextParticipants: nextParticipants
                                           turnTimeout: GKTurnTimeoutDefault
                                             matchData: matchData
                                     completionHandler: ^(NSError *error) {
                                           // do something like refreshing UI
                                     } ];
    

    然而,我的问题的第二部分仍然有效。我不清楚如何有条件地进行自动匹配(例如:我愿意与想要使用一级方程式赛车但不想使用拉力赛车的人进行自动匹配)。

    【讨论】:

      【解决方案2】:

      关于第二部分(未回答),我认为GKMatchRequest.playerAttributes 的目的是实现您想要的。您设置一些值来标识所选比赛的属性,游戏中心确保匹配的参与者对相同的游戏选项感兴趣。所以,如果你有一款赛车游戏,你可以在不同的天气条件下比赛汽车、自行车和船只,你可能会拥有这样的游戏:

      typedef enum GameType {
          GameTypeRaceCars = 0;
          GameTypeMotorbikes = 1;
          GameTypeBoats = 2;
      };
      typedef enum GameOptionWeather {
          GameOptionWeatherSunny = 0;
          GameOptionWeatherRainy = 1;
      };
      GKMatchRequest *request = /* boilerplate */
      // bit 0-1 = GameType.
      // bit 3   = Weather option.
      request.playerAttributes = (uint32_t)GameTypeBoats;
      request.playerAttributes |= (uint32_t)(GameOptionWeatherRainy) << 2;
      
      /* boilerplate to create the match with the request */
      

      这将确保任何匹配的玩家也想在雨中赛艇(谁不想?)。

      至少,我认为你应该这样做。不过老实说,到目前为止,我自己还未能让 Game Center 回合制匹配系统在沙盒中运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-15
        • 2011-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多