gpt4 book ai didi

ios - 在 iOS 中将高分上传到游戏中心排行榜时需要帮助来修复我的代码

转载 作者:行者123 更新时间:2023-11-30 13:49:47 25 4
gpt4 key购买 nike

经过一些研究,我认为这是正确的代码。一切似乎都正常,并且没有错误。上传的第一个高分显示在排行榜上(对于我用于测试的两个游戏中心帐户),但在初始高分(应该替换它)之后上传的任何分数都不会显示(我尝试过使用多个帐户) ,同样的问题)。这是我的代码(当按下按钮时我调用此函数)。一切加载正常,游戏中心窗口完美关闭,没有任何错误,但帐户 1 的分数仍为 0(这是加载的第一个分数),而帐户 2 的分数仍为 2(这是加载的第一个分数)。 (highScore是调用此函数之前保存的NSInteger)请帮忙!

 func saveHighscore() {

//check if user is signed in
if GKLocalPlayer.localPlayer().authenticated {


let gameScoreReporter = GKScore(leaderboardIdentifier: "color_runner_leaderboard")

gameScoreReporter.value = Int64(highScore)

let scoreArray1: [GKScore] = [gameScoreReporter]

GKScore.reportScores(scoreArray1, withCompletionHandler: {(NSError) -> Void in
if NSError != nil {
print(NSError!.localizedDescription)
} else {
print("completed Easy")
}

})

}

}

此代码显示排行榜:

 //show leaderboard screen
func showLeader() {
let vc = self.view?.window?.rootViewController
let gc = GKGameCenterViewController()
gc.gameCenterDelegate = self
vc?.presentViewController(gc, animated: true, completion: nil)
}

此代码用于上传分数(在加载第一个分数后似乎不起作用)并显示排行榜

let highScoreDefault = NSUserDefaults.standardUserDefaults()
if highScoreDefault.valueForKey("HighScoreSaved") != nil {
highScore = highScoreDefault.valueForKey("HighScoreSaved") as! NSInteger!
[saveHighscore()]
}




[showLeader()]

最佳答案

试试这个:

 func saveHighScore(identifier: String, score: Int) {

if (GKLocalPlayer.localPlayer().authenticated) {

let scoreReporter = GKScore(leaderboardIdentifier: identifier)
scoreReporter.value = Int64(score)

let scoreArray:[GKScore] = [scoreReporter]

GKScore.reportScores(scoreArray, withCompletionHandler: {

error -> Void in

if (error != nil) {

print("error")
}
else {
print("Posted score of \(score)")
}
})
}
}

当你想保存高分时,添加以下代码:

self.saveHighScore("yourLeaderboardName", 得分:GameState.sharedInstance.highScore)

添加此类。 GameState.swift:

import Foundation

class GameState {
var score: Int
var highScore: Int
var coins: Int

init() {
// Init
score = 0
highScore = 0
coins = 0

// Load game state
let defaults = NSUserDefaults.standardUserDefaults()

highScore = defaults.integerForKey("highScore")
coins = defaults.integerForKey("coins")
}

func saveState() {
// Update highScore if the current score is greater
highScore = max(score, highScore)

// Store in user defaults
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(highScore, forKey: "highScore")
defaults.setInteger(coins, forKey: "coins")
NSUserDefaults.standardUserDefaults().synchronize()
}

class var sharedInstance: GameState {
struct Singleton {
static let instance = GameState()
}

return Singleton.instance
}
}

并将这个辅助类添加到它自己的 Swift 文件中。不确定是否需要。我称之为 GameKitHelper.swift:

import UIKit
import Foundation
import GameKit

let PresentAuthenticationViewController = "PresentAuthenticationViewController"

class GameKitHelper: NSObject {

static let sharedInstance = GameKitHelper()


var authenticationViewController: UIViewController?
var gameCenterEnabled = false

func authenticateLocalPlayer() {

//1
let localPlayer = GKLocalPlayer()
localPlayer.authenticateHandler = {(viewController, error) in

if viewController != nil {
//2
self.authenticationViewController = viewController

NSNotificationCenter.defaultCenter().postNotificationName(PresentAuthenticationViewController, object: self)
} else if error == nil {
//3
self.gameCenterEnabled = true
}
}

}

func reportAchievements(achievements: [GKAchievement], errorHandler: ((NSError?)->Void)? = nil) {
guard gameCenterEnabled else {
return
}

GKAchievement.reportAchievements(achievements, withCompletionHandler: errorHandler)
}

func showGKGameCenterViewController(viewController: UIViewController) {
guard gameCenterEnabled else {
return
}

//1
let gameCenterViewController = GKGameCenterViewController()

//2
gameCenterViewController.gameCenterDelegate = self

//3
viewController.presentViewController(gameCenterViewController, animated: true, completion: nil)
}

func saveHighScore(identifier: String, score: Int) {

if (GKLocalPlayer.localPlayer().authenticated) {

let scoreReporter = GKScore(leaderboardIdentifier: identifier)
scoreReporter.value = Int64(score)

let scoreArray:[GKScore] = [scoreReporter]

GKScore.reportScores(scoreArray, withCompletionHandler: {

error -> Void in

if (error != nil) {

print("error")
}
else {

print("Posted score of \(score)")
}
})
}
}
}

extension GameKitHelper: GKGameCenterControllerDelegate {
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
}

关于ios - 在 iOS 中将高分上传到游戏中心排行榜时需要帮助来修复我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34426775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com