2

通过使用 GKTurnbaseMatch 默认匹配方案,我能够连接到匹配。我还可以通过编程方式和使用默认视图控制器创建一个新的匹配项。我还可以通过编程方式和使用默认视图控制器找到在 Game Center 中创建本地用户的匹配列表。但是总是提到轮到我,因为我没有和下一个参与者叫 end turn。1. 匹配连接后,调用 didFindMatch 并且接收到的匹配对象不打印预期的第二个玩家的 ID,而是将该玩家的状态打印为“匹配”。

 //#3
func turnBasedMatchmakerViewController(_ viewController: GKTurnBasedMatchmakerViewController,
didFind match: GKTurnBasedMatch)
{
    print(match)
    match.loadMatchData { (d, e) in

        print(d)
        print(e)

    }


    self.dismiss(animated: true, completion: nil)
    //performSegue(withIdentifier: "GamePlayScene", sender: match)
}

此匹配打印如下。

[
<GKTurnBasedParticipant 0x60000000c430 - playerID:G:25153527799 (local player) status:Active matchOutcome:None lastTurnDate:(null) timeoutDate:(null)>,
<GKTurnBasedParticipant 0x60000000cd50 - playerID:(null)   status:Matching matchOutcome:None lastTurnDate:(null) timeoutDate:(null)>

]

我怎么能连接两个玩家。

  1. 我可以找到本地玩家创建的所有比赛,有没有办法在游戏中心找到任何人创建的空比赛列表。

  2. 如何使用比赛 ID 加入比赛?

4

1 回答 1

1

这个答案陈述了上述声明中询问的一些非常基本的问题,特别是针对那些将来可能面临类似障碍的人。首先,非常重要的一点是,GKTurnBasedMatchs不会为新的比赛填满比赛参与者数组中的空白位置,除非你轮到你了。这就是为什么我在两台设备上都创建新匹配但无法同时匹配两者时遇到麻烦的原因。因此,双方的参与者阵列正在打印我在上述问题中实际提出的问题。它正在打印两个设备的第一个参与者作为本地播放器。幸运的是,在阅读了本教程之后,我发现比赛的创建者必须通过结束轮到辞职才能使比赛对其他搜索者开放。这样,只有新参与者才能找到您的比赛的空位。

现在在结束你的回合之前,你必须选择下一回合的参与者,虽然你的match.participants数组将有虚拟参与者,它们的状态为 match 并且 playerIDs 为 absolute null,但你必须假设他们是你比赛的其他玩家。

因此,您必须将下一个参与者分配到您将在轮到结束时放入的数组的索引 0。此外,您必须第一次使用您的匹配状态创建一个新的数据对象,因为在此之前还会有匹配数据null

在我的情况下,这是类型myMatch.endTurn的虚拟数据Dictionary

 @IBAction func endMyTurn(_ sender: Any) {

    // This is sincerely for Ending Turn
    var dictionaryExample = [String:Any]()
    dictionaryExample = ["name": "Asim", "value": 2]



    let dataToSend: Data = NSKeyedArchiver.archivedData(withRootObject: dictionaryExample)



    if (myMatch?.participants?.count)! > 0
    {

        // Match is started
        var pArray : [GKTurnBasedParticipant] = myMatch!.participants!
        let currenParticipantIndex : Int = (myMatch!.participants?.index(of: myMatch!.currentParticipant!))!

        pArray.remove(at: currenParticipantIndex)

        let currentParticipant = myMatch?.currentParticipant
        pArray.append(currentParticipant!)

        myMatch?.endTurn(withNextParticipants: pArray, turnTimeout: GKTurnTimeoutDefault, match: dataToSend, completionHandler: { (e) in
            print(e ?? "No Error:")
        })

    }
    else
    {
        // No Match


    }





}

现在,在你的回合结束后,你的查找比赛将首次开始显示Their Turn这场比赛,并且在你的对手进一步结束回合之前不允许你玩。在此处输入图像描述

Asim K.是分配到您比赛的空位的其他玩家。

注意:这将在找出下一个玩家之后显示,并且在真实比赛中可能需要一段时间,但在测试中它只会在您使用不同帐户Game Centre测试您的应用时才会发生。Game Centre

**注意:这将保持原样,直到下一个玩家轮到他/她。**

对于玩家 2 找到匹配,这会发生。turnBasedMatchmakerViewController(_ viewController: GKTurnBasedMatchmakerViewController, didFind match: GKTurnBasedMatch)当从列表中找到/选择匹配时,将调用匹配数据和完全填充的参与者数组(在我的情况下只有 2 个)。

//#3
func turnBasedMatchmakerViewController(_ viewController: GKTurnBasedMatchmakerViewController,
didFind match: GKTurnBasedMatch)
{
    print(match)
    myMatch = match
    match.loadMatchData { (d, e) in

        print(d ?? "No Data:")
        let recievedData: [String: Any] = (NSKeyedUnarchiver.unarchiveObject(with: d!) as? [String : Any])!


        // Check if data contains : "name" .. "value"
        if recievedData["name"] != nil && recievedData["value"] != nil
        {

            let name = recievedData["name"] as! String
            let val = recievedData["value"] as! Int

            print("First Item: \(name)")
            print("Second Item: \(val)")

        }


        print(e ?? "No Error:")

    }


    self.dismiss(animated: true, completion: nil)
    //performSegue(withIdentifier: "GamePlayScene", sender: match)
}

这是玩家 2 的最终输出。 在此处输入图像描述

于 2017-07-02T05:47:18.543 回答