簡體   English   中英

創建/加入比賽后,GKTurnBasedMatch將第二球員狀態打印為比賽狀態嗎?

[英]GKTurnBasedMatch after creating/joining a match prints 2nd player status as matching?

通過使用GKTurnbaseMatch默認對接方案,我可以連接到一個匹配項。 我還可以通過編程和使用默認視圖控制器來創建新的匹配項。 我還可以找到以編程方式和默認視圖控制器在Game Center中創建本地用戶的比賽列表。 但是總有提到我的回合,因為我還沒有跟下一個參與者叫回合。 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的比賽?

該答案陳述了上述陳述中所審問的一些非常基本的問題,特別是針對那些將來可能面臨類似障礙的人們。 首先,非常重要的一點是, GKTurnBasedMatchs不會填充新參與者的參與者數組中的空白,除非您輪到自己。 這就是為什么我在兩個設備上都創建新匹配項時遇到了麻煩,但無法同時匹配這兩個原因。 因此,雙方的參與者都在打印我在上述問題中實際詢問的內容。 它正在將兩個設備的第一個參與者打印為本地播放器。 幸運的是,在閱讀了本教程之后 ,我發現比賽的創造者必須必須退出比賽並辭職,以使比賽對其他尋求者開放。 這樣,只有新的參與者才能找到比賽的空位。

現在,在結束您的回合之前,您必須選擇下一個回合的參與者,盡管match.participants數組將具有虛擬參與者,每個參與者的狀態均為匹配,並且match.participants絕對為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的情況下的最終輸出。 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM