1

假设我有一个有 2 个玩家的 GKTurnBasedMatch,第二个在他的回合内被没收。我应该如何向第一个知道游戏已经结束的用户显示?或者也许让第一个玩家以编程方式退出?

另一个 GKTurnBasedMatch——这次是 12 名玩家。我不明白这里的一件事 - 说玩家 7 退出,这意味着当轮到他时,它会卡住游戏,我需要以编程方式结束对所有用户的比赛?或者也许 GC 会相应地重新编号剩余的球员?

提前致谢!

4

2 回答 2

0

您需要将以下内容之一发送到您的 GKTurnBasedMatch 对象:

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

- (void)participantQuitOutOfTurnWithOutcome:(GKTurnBasedMatchOutcome)matchOutcome
                      withCompletionHandler:(void (^)(NSError *error))completionHandler

呼叫participantQuitOutOfTurnWithOutcome将向比赛中的其他玩家发送回合事件,以通知他们有玩家退出。match.participants 中玩家的对象会有 matchOutcomeGKTurnBasedMatchOutcomeQuit

于 2013-02-18T08:58:21.453 回答
0

我创建了一个基于游戏工具包回合的比赛示例项目,该项目说明了不按顺序退出和依次退出。查看文件中的quit()函数GameModel.swift以了解如何调用这些函数:

func quit(completionHandler: @escaping (Error?) -> Void) {
    if isLocalPlayerTurn {
        let next = nextParticipants()
        let data = NSKeyedArchiver.archivedData(withRootObject: self)
        match?.participantQuitInTurn(with: .quit, nextParticipants: next, turnTimeout: 600, match: data) { error in
            completionHandler(error)
        }
    } else {
        match?.participantQuitOutOfTurn(with: .quit) { error in
            completionHandler(error)
        }
    }
}

当然,检查某人是否赢了也很重要。这是checkForWin()来自同一个文件的函数。

func checkForWin(completionHandler: @escaping (Bool, Error?) -> Void) {
    guard let stillPlaying = match?.participants?.filter({ $0.matchOutcome == .none }),
        stillPlaying.count == 1,
        stillPlaying[0].player?.playerID == currentPlayerID
        else {
            return completionHandler(false, nil)
    }

    stillPlaying[0].matchOutcome = .won
    let data = NSKeyedArchiver.archivedData(withRootObject: self)

    match?.endMatchInTurn(withMatch: data) { error in
        print("***** match ended")
        completionHandler(true, error)
    }
}

所有这些在整个示例项目的上下文中都更有意义。我希望它有所帮助。

于 2017-07-27T22:12:49.407 回答