【问题标题】:GKTurnBasedMatch after creating/joining a match prints 2nd player status as matching?创建/加入比赛后的 GKTurnBasedMatch 将第二名玩家状态打印为匹配?
【发布时间】:2017-07-02 13:47:18
【问题描述】:

通过使用 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 的匹配?

【问题讨论】:

    标签: ios iphone gamekit gkturnbasedmatch


    【解决方案1】:

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

    现在在结束你的回合之前,你必须选择下一回合的参与者,尽管你的match.participants 数组将有虚拟参与者,其状态与每个参与者匹配,并且玩家 ID 肯定为 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
    
    
        }
    
    
    
    
    
    }
    

    现在在你的回合结束后,你的 find 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 的最终输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      相关资源
      最近更新 更多