【问题标题】:Creating a GKTurnBasedMatch with a mix of invited and auto-match players创建一个包含受邀玩家和自动匹配玩家的 GKTurnBasedMatch
【发布时间】:2014-04-15 07:32:09
【问题描述】:

Game Center 的回合制匹配界面允许用户与多个玩家创建匹配,并在其中一些位置添加邀请好友,而将其他位置留给自动匹配。

我正在创建我们自己的游戏内匹配创建界面,到目前为止,它运行良好,除了创建与受邀玩家和自动匹配玩家的匹配。

GKMatchmaker 有 addPlayersToMatch 方法,我相信您可以在匹配存在后添加自动匹配玩家,但 GKTurnBasedMatch 没有等效方法。

以下是我正在使用的代码,它工作得很好。如果有人知道如何添加许多自动匹配玩家,将不胜感激!

- (GKMatchRequest *) buildMatchRequestWithFriends: (NSArray *) friendsList NumberOfPlayers: (NSInteger) numberOfPlayers
{
    NSLog(@"TurnByTurnHelper.buildMatchRequestWithFriends");
    GKMatchRequest *request = [[GKMatchRequest alloc] init];
    NSArray *playersToInvite = [NSArray arrayWithArray:friendsList];
    request.playersToInvite = playersToInvite;
    request.defaultNumberOfPlayers = numberOfPlayers + 1;
    return request;
}

- (void) requestMatchWithFriends:(NSArray *) friendsList NumberOfPlayers: (NSInteger) numberOfPlayers{

    if (!_delegate)
    {
        NSLog(@"Error: Expected but did not find delegate");
        return;
    }

    GKMatchRequest *request = [self buildMatchRequestWithFriends:friendsList NumberOfPlayers: numberOfPlayers];

    [GKTurnBasedMatch findMatchForRequest: request withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error)
     {
         if (match){
             NSLog(@"findMatchForRequest: Success!");

             // Add match to matches

         } else {
             NSLog(@"error: %@", error);

         }
     }];
}

【问题讨论】:

    标签: ios game-center gamekit gkturnbasedmatch


    【解决方案1】:

    好吧,经过一番搜索和测试,我发现 Apple 开发人员文档不完整和/或具有误导性。 GKMatchRequest的playersToInvite属性部分做了如下声明:

    "如果该属性的值为非 nil,则当您使用请求创建比赛时,Game Center 会邀请这些玩家参加比赛。不进行任何自动处理,并且忽略 GKMatchRequest maxPlayers 和 minPlayers 属性。"

    这适用于 GKMatchmaker,但不适用于 GKTurnBasedMatch!对于 GKTurnBasedMatch,如果您在 playerToInvite 属性中提供一组 playerId,则不会忽略 maxPlayers 和 minPlayers 属性,并且 Game Center 会用随机玩家填充额外的空间。

    下面的代码可以解决问题:

    - (GKMatchRequest *) buildMatchRequestWithFriends: (NSArray *) friendsList NumberOfPlayers: (NSInteger) numberOfPlayers
    {
        GKMatchRequest *request = [[GKMatchRequest alloc] init];
        NSArray *playersToInvite = [NSArray arrayWithArray:friendsList];
    
        if([friendsList count]<=0){
            request.minPlayers = 2;
            request.maxPlayers = numberOfPlayers;
            request.playersToInvite = nil;
        } else {
            request.minPlayers = 2;
            request.maxPlayers = numberOfPlayers;
            request.playersToInvite = playersToInvite;
        }
    
        return request;
    }
    

    这个故事的寓意是:有时最好不要使用 RTFM!

    【讨论】:

    • 我现在正在玩游戏中心,我认为这条评论只是节省了我几个小时的工作时间。游戏中心太奇怪了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    相关资源
    最近更新 更多