OGeek|极客世界-中国程序员成长平台

标题: iphone - 根据 ID 加入特定的 GKTurnBasedMatch [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:32
标题: iphone - 根据 ID 加入特定的 GKTurnBasedMatch

我想允许我游戏的玩家加入特定的比赛。例如,PlayerA 通过 findMatchForRequest 启动 GKTurnBasedMatch。然后他希望他的 friend 加入,但不希望他的 friend 在游戏中心搜索他,PlayerA 想将 matchID 发送给 PlayerB (比如说,通过社交媒体或其他方式......我的目标实际上是让玩家使用自定义 URL 模式将游戏链接发送给 friend ,例如,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?
    }
  }];



Best Answer-推荐答案


我最终解决了这个问题,而且非常简单。诀窍是,一旦您加载 GKTurnBasedMatch,您只想查看找到的匹配的参与者(这些是游戏中已经存在的玩家),然后从中创建一组玩家 ID。您可以在执行 findMatchForRequest 时将此数组用作 .playersToInvite 属性。

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

这个函数会让玩家加入特定的matchID:

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

关于iphone - 根据 ID 加入特定的 GKTurnBasedMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18772576/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4