在Swift中调用成就屏幕

时间:2015-08-09 20:00:30

标签: swift game-center leaderboard achievements

我在游戏中心调用成就屏幕时遇到了一些极端困难。我已经在iTunes连接中设置了成就,如果我首先通过排行榜访问成就屏幕,它会弹出。然而;我希望能够按下特定的成就按钮并直接指向成就屏幕。任何人都可以帮忙吗?我在互联网上搜索了高低(and read through all of the documentation)。我已经找到了很多用于实现排行榜的资源,但是在swift中实现成就的资源并不多。我的代码如下。对我最近两个职能的任何建议?

override func viewDidLoad() {
    super.viewDidLoad()

    login()

}

func login() {
    println("Game Center Login Called")
    let localPlayer = GKLocalPlayer.localPlayer()

    // Handle the authentication
    localPlayer.authenticateHandler = {(Home: UIViewController!, error: NSError!) -> Void in
        if Home != nil {
            println("Authentication is being processed.")
            self.presentViewController(Home, animated: true, completion: nil)

        } else {
            println("Player has been successfully authenticated.")
        }
    }

}

func showLeaderboard() {


    let gkScore = GKScore(leaderboardIdentifier: "high_Score_Leader_Board")
    gkScore.value = Int64(highscore)
    GKScore.reportScores([gkScore], withCompletionHandler: ( { (error: NSError!) -> Void in
        if (error != nil) {
            // handle error
            println("Error: " + error.localizedDescription);
        } else {
            println("Score reported: \(gkScore.value)")
        }
    }))


    var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
    gcViewController.gameCenterDelegate = self

    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards

    gcViewController.leaderboardIdentifier = "high_Score_Leader_Board"
    self.showViewController(gcViewController, sender: self)
    self.presentViewController(gcViewController, animated: true, completion: nil)
}





@IBAction func gameCenterButtoPressed(sender: AnyObject) {
    showLeaderboard()
}



func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!)
{
    self.dismissViewControllerAnimated(true, completion: nil)
}

func showAchievements() {
    // show Achievements screen
}


@IBAction func achievementButtonPressed(sender: AnyObject) {

    // Call show achievements function when button pressed
}

1 个答案:

答案 0 :(得分:2)

而不是:

gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards

我认为你想要的是:

gcViewController.viewState = GKGameCenterViewControllerState.Achievements

我发现了这些信息in this related tutorial