【问题标题】:How to fetch top 10 scores from a specific gamecenter leaderboard with swift (iOS)如何使用 swift (iOS) 从特定游戏中心排行榜中获取前 10 名分数
【发布时间】:2016-02-10 07:44:34
【问题描述】:

如何在 swift (iOS) 中获取特定游戏中心排行榜的前 10 名?

我想从排行榜中获取前 10 名的分数和玩家,并在游戏中建立一个自定义的“名人堂”。 如何从leaderbaord获取数据?

【问题讨论】:

    标签: ios swift game-center leaderboard


    【解决方案1】:

    同时我找到了答案:

        let leaderBoardRequest = GKLeaderboard()
        leaderBoardRequest.identifier = kGcIdHighScore // my GC Leaderboard ID
        leaderBoardRequest.playerScope = GKLeaderboardPlayerScope.Global
        leaderBoardRequest.timeScope = GKLeaderboardTimeScope.AllTime
        leaderBoardRequest.range = NSMakeRange(1,10) // top 10
    
        leaderBoardRequest.loadScoresWithCompletionHandler
            { (scores, error) -> Void in
                if (error != nil)
                    {
                        print("Error: \(error!.localizedDescription)")
                    } else if (scores != nil)
                      {
    
                      print("got top \(scores?.count) scores"  )
    
                        var ii = 0
                        while (ii < scores?.count)
                        {
                            NSLog("%i ....... %@ %i %i", Int(ii+1), scores![ii].player.alias! , scores![ii].rank , scores![ii].value )
                            ii = ii + 1
                        }
    
                      }
            } // end leaderBoardRequest.loadScoresWithCompletionHandler
    

    【讨论】:

      【解决方案2】:

      我已使用上述内容并对其进行了调整以与 Swift 4 一起使用 我创建了一个函数来获取其中的分数我将结果附加到一个元组数组中,其中: 0元素是实现的地方 1-element 是 GameCenter 玩家名称 2-元素是分数

      我用它来将所有内容填充到 tableView 中。根据您的需要进行调整

      import GameKit
      import UIKit 
      
      class HighScoresViewController: UIViewController, GKGameCenterControllerDelegate, UITableViewDelegate, UITableViewDataSource {
      
      @IBOutlet weak var scoreTableView: UITableView!
      
      var highScores = [(String,String,String)]()
      let LEADERBOARD_ID = "com.yourLeaderboard"
      
      override func viewDidLoad() {
          super.viewDidLoad()
          scoreTableView.delegate = self
          scoreTableView.dataSource = self
          getScores()
      }
      
      func getScores(){
           let leaderBoardRequest = GKLeaderboard()
           leaderBoardRequest.identifier = LEADERBOARD_ID // my GC Leaderboard ID
           leaderBoardRequest.playerScope = GKLeaderboardPlayerScope.global
           leaderBoardRequest.timeScope = GKLeaderboardTimeScope.allTime
           leaderBoardRequest.range = NSMakeRange(1,10) // top 10
      
           leaderBoardRequest.loadScores
              { (scores, error) -> Void in
                  if (error != nil)
                  {
                      print("Error: \(error!.localizedDescription)")
                  } else if (scores != nil)
                  {
                      print("Top \(scores?.count ?? 0) scores"  )
      
                      var ii = 0
                      while (ii < scores?.count ?? 0)
                      {
                          self.highScores.append(("\(scores![ii].rank)" , "\(scores![ii].player?.alias! ?? "Player")" , "\(scores![ii].value)"))
                          print(Int(ii+1) , scores![ii].rank , scores![ii].player?.alias! ?? "Player" , scores![ii].value )
                          ii = ii + 1
                      }
                      self.refreshTableData()                }
          } // end leaderBoardRequest.loadScoresWithCompletionHandler
      }
      
      func refreshTableData(){
      
          print("refreshed")
          self.scoreTableView.reloadData()
        }
      func numberOfSections(in tableView: UITableView) -> Int {
          return 1
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "scoreCell", for: indexPath) as! ScoreTableViewCell
          cell.placeAchieved.text = highScores[indexPath.row].0
          cell.playerName.text = highScores[indexPath.row].1
          cell.playerScore.text = highScores[indexPath.row].2
          return cell
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return highScores.count
        }
      }
      

      这是我的由标签组成的 TableViewCell 文件

      import UIKit
      
      class ScoreTableViewCell: UITableViewCell {
         @IBOutlet weak var placeAchieved: UILabel!
         @IBOutlet weak var playerName: UILabel!
         @IBOutlet weak var playerScore: UILabel!
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-06
        • 2021-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多