gpt4 book ai didi

swift - GameCenter 分数未发布到排行榜

转载 作者:行者123 更新时间:2023-12-04 08:28:16 27 4
gpt4 key购买 nike

在这里耐心等待,因为有很多东西要解释!
我正在努力在我的游戏中实现一个 Game Center 高分排行榜。我已经环顾四周寻找如何正确实现此代码的示例,但找不到很多 Material 。因此,我尝试根据我在苹果文档中找到的信息自己实现它。
长话短说,当我更新我的分数时,我得到了成功打印,但实际上没有发布任何分数(或者至少在打开时没有分数显示在 Game Center 排行榜上)。
在我展示代码之前,我质疑的一件事是这个游戏仍在开发中。在 AppStoreConnect 中,排行榜的状态为“未上线”。这会影响发布的分数吗?
上代码。我创建了一个 GameCenter 类,用于处理获取排行榜并将分数发布到特定排行榜。我将完整发布代码,并将在下面讨论正在发生的事情。

class Leaderboard
{
var id : String
var leaderboard : GKLeaderboard
var loaded : Bool

init()
{
self.id = ""
self.leaderboard = GKLeaderboard()
self.loaded = false
}

init(id: String, leaderboard: GKLeaderboard, loaded: Bool)
{
self.id = id
self.leaderboard = leaderboard
self.loaded = loaded
}
}

class GameCenter
{
static let shared = GameCenter()

private var player = GKLocalPlayer.local
private var leaderboards : [Leaderboard] = []

func authenticatePlayer()
{
player.authenticateHandler = { (vc, error) -> Void in
if let error = error
{
print(error.localizedDescription)
}
else if let vc = vc
{
if let viewController = UIApplication.shared.windows.first!.rootViewController
{
viewController.present(vc, animated: true)
{
self.loadLeaderboards()
}
}
}
else
{
self.loadLeaderboards()
}
}
}

func loadLeaderboards()
{
var leaderboardIDs : [String] = []

// Gather all of the leaderboard ids that we have
for leaderboard in GameCenterLeaderboards.allCases
{
leaderboardIDs.append(leaderboard.rawValue)
}

// Load the leaderboard for all of these ids and add to a new array
GKLeaderboard.loadLeaderboards(IDs: leaderboardIDs) { (loadedLeaderboards, error) in

if let error = error
{
print(error.localizedDescription)
return
}

if let loadedLeaderboards = loadedLeaderboards
{
print("\n--- Loaded Leaderboards ---")
for loadedBoard in loadedLeaderboards
{
let board = Leaderboard(id: loadedBoard.baseLeaderboardID, leaderboard: loadedBoard, loaded: true)
self.leaderboards.append(board)

print("ID: \(board.id)")
}
print("\n")

self.updateLocalHighScore()
}
}
}

func playerAuthenticated() -> Bool
{
return player.isAuthenticated
}

func submitScore(id: String)
{
if ( playerAuthenticated() )
{
let leaderboard = getLeaderboard(id: id)
if ( leaderboard.loaded )
{
print("Submitting score of \(AppSettings.shared.highScore!) for leaderboard \(leaderboard.id)")
leaderboard.leaderboard.submitScore(AppSettings.shared.highScore, context: -1, player: player) { (error) in
if let error = error
{
print(error.localizedDescription)
}
else
{
print("Successfully submitted score to leaderboard")
}
}
}
}
}

func getLeaderboard(id: String) -> Leaderboard
{
if let leaderboard = leaderboards.first(where: { $0.id == id } )
{
return leaderboard
}

return Leaderboard()
}

func updateLocalHighScore()
{
let leaderboard = getLeaderboard(id: GameCenterLeaderboards.HighScore.rawValue)
if ( leaderboard.loaded )
{
leaderboard.leaderboard.loadEntries(for: [player], timeScope: .allTime) { (playerEntry, otherEntries, error) in

if let error = error
{
print(error.localizedDescription)
return
}

if let score = playerEntry?.score
{
print("Player Score in leaderboard: \(score)")
if( score > AppSettings.shared.highScore )
{
AppSettings.shared.highScore = score
print("High Score Updated!")
}
else
{
// Lets post the local high score to game center
self.submitScore(id: leaderboard.id)
print("Local High Score Is Greating, requesting a submit!")
}
}
}
}
}
}
在另一个 GameScene 中,一旦游戏结束,我请求使用以下代码行向 Game Center 发布新的高分:
GameCenter.shared.submitScore(id: GameCenterLeaderboards.HighScore.rawValue)
我最后有疑问的是提交分数时的上下文。根据文档,这似乎只是 GameCenter 不关心的元数据,而是开发人员可以使用的东西。因此,我认为我可以将其划为导致问题的原因。
我相信我正确地实现了这一点,但由于某种原因,没有任何内容发布到排行榜。这是很多,但我想确保我把所有的想法都记下来。
关于为什么不发布的任何帮助都会很棒!非常感谢!
标记

最佳答案

我遇到了同样的问题 - 使用 iOS 14 提供的新 GameKit API 向 Game Center 提交分数实际上从未保存在 Game Center 端(即使没有报告任何错误)。
最后对我有用的解决方案只是使用 Type Method (使用我的排行榜ID):

class func submitScore(_ score: Int, 
context: Int,
player: GKPlayer,
leaderboardIDs: [String],
completionHandler: @escaping (Error?) -> Void)
而不是 Instance Method对应物(显然在 Apple 方面有一些错误):
func submitScore(_ score: Int, 
context: Int,
player: GKPlayer,
completionHandler: @escaping (Error?) -> Void)
我为此发疯了,所以我希望这对其他人有所帮助。

关于swift - GameCenter 分数未发布到排行榜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65153511/

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