如何为玩家列出所有可用的GKTurnBasedMatches? [英] How to list all available GKTurnBasedMatches for a player?

查看:124
本文介绍了如何为玩家列出所有可用的GKTurnBasedMatches?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Game Center的回合制比赛来构建游戏。

I'm building a game using Game Center's turn based matches.

我想显示所有可用匹配项的列表。我已尝试使用 loadMatchesWithCompletionHandler(),但游戏数组返回 nil ,错误也会返回为。有一些正在进行的比赛。

I want to display a list of all the available matches. I've tried using loadMatchesWithCompletionHandler(), but the array of games returns as nil, and the error also returns as nil. There are some ongoing matches.

这是我到目前为止:

func authenticateLocalUser() {
    if !gameCenterAvailable { return }

    let player = GKLocalPlayer.localPlayer()
    if player.authenticated == false {
        player.authenticateHandler = {(viewController, error) -> Void in
            if viewController != nil && self.presentingViewController != nil
            {
                self.presentingViewController!.presentViewController(viewController!, animated: true, completion: {
                    GKLocalPlayer.localPlayer().registerListener(self)

                    GKTurnBasedMatch.loadMatchesWithCompletionHandler({games, error in
                        print(games)
                        if games != nil {
                            print(games!.count)
                        }else {
                            print(error)
                        }
                    })
                })
            } else {

                if player.authenticated == true {
                    GKLocalPlayer.localPlayer().registerListener(self)

                    GKTurnBasedMatch.loadMatchesWithCompletionHandler({games, error in
                        print(games)
                        if games != nil {
                            print(games!.count)
                        }else {
                            print(error)
                        }
                    })
                }
            }
        }
    } else {
        print("already authenticated")
    }
}

创建新匹配时我甚至得到 nil (它会打印我刚刚创建的匹配):

I even get nil when creating a new match (it will print the match I just created, though):

func findMatchWith(minPlayers: Int, maxPlayers: Int) {
    if !gameCenterAvailable { return }

    let request = GKMatchRequest()
    request.minPlayers = minPlayers
    request.maxPlayers = maxPlayers
    request.defaultNumberOfPlayers = 2

    GKLocalPlayer.localPlayer().loadFriendPlayersWithCompletionHandler({players, error in
        if error != nil {return}
        request.recipients?.append(players![0])

        GKTurnBasedMatch.findMatchForRequest(request, withCompletionHandler: { match, error in
            if error != nil {
                print(error?.localizedDescription)
                return
            }
            print(match)

            GKTurnBasedMatch.loadMatchesWithCompletionHandler({games, error in
                print(games)
                if games != nil {
                    print(games!.count)
                }else {
                    print(error?.localizedDescription)
                } 
            })
        })
    })
}


推荐答案

这不是代码。这就是在iTunes Connect中设置游戏的方式。我需要这样做:

It wasn't the code. It was how the game was set up in iTunes Connect. I needed to do this:


  1. 转到我的应用程序> App Store>准备提交并切换游戏中心的开关

  2. 添加我之前在功能下创建的排行榜

稍后,我将尝试删除排行榜,看看它是否仍然有效。实际的应用程序不会有排行榜。

Later, I will try to delete the leaderboard, and see if it still works. The actual app won't have a leaderboard.

我的困惑是因为我没有得到无法识别的游戏错误,我能够创建比赛,玩转,列出玩家的朋友,但不列出匹配。

My confusion was because I wasn't getting the "unrecognized game" error, and I was able to create matches, play turns, list player's friends, but not list matches.

这篇关于如何为玩家列出所有可用的GKTurnBasedMatches?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆