首页 文章

Game Center的自动匹配和endTurnWithNextParticipants

提问于
浏览
5

我正在开发一个有两个游戏中心玩家的回合制游戏,我想允许自动匹配 .

我已经读到了,为了实际发送给玩家的邀请,邀请玩家必须结束他/她的回合 . 这意味着调用此方法:

- (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);
                 }];

如果阵列为零,我不知道谁将参加比赛,我怎么可能将转牌传给下一位球员?如果我在nextParticipants参数中使用nil,当然我得到一个'nextParticipants的无效列表'错误 .

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

所以,我也不明白的是自动匹配实际上是如何工作的 . 它会无条件地匹配任何已经开始与自动比赛进行新比赛的两名球员吗?我不能以某种方式选择我想要自动匹配的匹配项吗? (假设,例如,游戏允许几个难度级别,并且我不希望与在较低级别玩的人自动匹配) .

编辑(根据xcodegirl的评论):

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

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

但不好的是,playerGroup似乎不是GKTurnBasedMatch的可用属性 . 因此,如果您列出了您的匹配项,包括待处理的自动匹配项,并希望显示有关您要玩的游戏类型的信息,则应以其他方式存储此信息 .

2 回答

  • 0

    经过一些尝试,似乎这个问题的第一部分的答案如下 . 一旦比赛开始,即使没有人匹配自动匹配邀请,参与者阵列也会填充所需数量的玩家(其中一个是邀请玩家),每个缺少的玩家都是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
                                     } ];
    

    然而,我的问题的第二部分仍然存在 . 我不清楚如何有条件地进行自动匹配工作(例如:我愿意与想要参加一级方程式赛车的人进行自动匹配,而不是与拉力赛车进行比赛) .

  • 6

    关于第二个(未答复的)部分,我认为 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 */
    

    这将确保任何匹配的球员也想在雨中赛艇(谁没有?) .

    至少,我认为's how you'应该这样做 . 虽然说实话,到目前为止,我已经失败了,让我自己在沙盒中使用Game Center .

相关问题