根据其ID加入特定的GKTurnBasedMatch

时间:2013-09-12 19:14:57

标签: iphone ios game-center gamekit

我想让我的游戏玩家加入特定的比赛。例如, PlayerA 通过GKTurnBasedMatch启动findMatchForRequest。然后他希望他的朋友加入,但不是希望他的朋友在游戏中心搜索他, PlayerA 想要将matchID发送到 PlayerB (比如说,通过社交媒体或其他......我的目标实际上是让玩家使用自定义URL架构向朋友发送游戏链接,例如mygame://join/**matchID**)。

从这里开始, PlayerB 显然可以加载GKTurnBasedMatch loadMatchWithID的匹配...但他怎么能明确请求加入呢?

[GKTurnBasedMatch loadMatchWithID:matchID withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error) {
    if(error || !match) {
      [[AMAlertManager sharedManager] showError:i18n(@"errors.invalidInvite")];
    }
    else {
       // Now what?
    }
  }];

1 个答案:

答案 0 :(得分:1)

我最终想出来了,这很简单。诀窍在于,一旦你加载GKTurnBasedMatch,你只想看看找到的匹配参与者(这些是已经在游戏中的玩家),然后从他们创建一个玩家ID数组。在执行.playersToInvite时,您可以将此数组用作findMatchForRequest属性。

事实上,您可以将此数组传递给handleInviteFromGameCenter委托方法,以重用游戏中心邀请的现有代码。

此功能可让玩家加入特定的matchID

- (void)handleInviteToMatchID:(NSString*)matchID {
  [GKTurnBasedMatch loadMatchWithID:matchID withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error) {
    if(error || !match) {
      [[AMAlertManager sharedManager] showError:i18n(@"errors.invite.invalid")];
    }
    else if(match.status != GKTurnBasedMatchStatusMatching) {
      [[AMAlertManager sharedManager] showError:i18n(@"errors.invite.notMatching")];
    }
    else {
      NSMutableArray *playersToInvite = [NSMutableArray array];
      for(GKTurnBasedParticipant *player in match.participants) {
        if(player.playerID) {
          [playersToInvite addObject:player.playerID];
        }
      }
      [self handleInviteFromGameCenter:playersToInvite];
    }
  }];
}