gpt4 book ai didi

swift - 如何快速将GameCenter放到应用程序上?

转载 作者:行者123 更新时间:2023-11-30 10:12:36 24 4
gpt4 key购买 nike

我使用 SpriteKit 和 Xcode 7 beta 制作了一个游戏。我尝试添加 GameCenter 和 Leaderboard,但问题是排行榜中的分数不会改变(HighScore 不会上传到 GameCenter),它始终保持 0,我不知道如何修复它。我使用 2 个不同的文件:GameViewController.swift 和 PointsLabel.swift

GameViewController.swift:

import GameKit

class GameViewController: UIViewController,UIGestureRecognizerDelegate, GKGameCenterControllerDelegate {

var score: PointsLabel!

override func viewDidLoad() {
super.viewDidLoad()

//initiate gamecenter
func authenticateLocalPlayer(){

let localPlayer = GKLocalPlayer.localPlayer()

localPlayer.authenticateHandler = {(GameViewController, error) -> Void in

if (GameViewController != nil) {
self.presentViewController(GameViewController!, animated: true, completion: nil)
}

else {
print((GKLocalPlayer.localPlayer().authenticated))
}
}
}
}

@IBAction func leaderboard(sender: UIButton) {
saveHighscore(score)
showLeader()
}


//send high score to leaderboard
func saveHighscore(score:Int) {

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

let scoreReporter = GKScore(leaderboardIdentifier: "Leaderboard_01")

scoreReporter.value = Int64(score)

let scoreArray: [GKScore] = [scoreReporter]

GKScore.reportScores(scoreArray, withCompletionHandler: {error -> Void in
if error != nil {
print("error")
}
})
}
}


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

//hides leaderboard screen
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController)
{
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)

}

此文件中有一个错误:

Cannot invoke 'saveHighscore' with an argument list of type '(PointsLabel!)'

代码:

@IBAction func leaderboard(sender: UIButton) {
saveHighscore(score) //<- Here is Error
showLeader()
}

PointsLabel.swift:

import Foundation
import UIKit
import SpriteKit

class PointsLabel: SKLabelNode {

var score:Int = 0

init(num: Int) {
super.init()

fontColor = UIColor.blackColor()
fontSize = 30.0

score = num
text = "\(num)"
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func increment() {
score++
text = "\(score)"
}

func setTo(num: Int) {
self.score = num
text = "\(self.score)"
}
}

我不知道如何解决它!

最佳答案

您的score变量的类型为PointsLabel但是你的saveHighscore函数需要 Int作为参数。

查看您的代码,变量 score将是PointsLabel的一个实例所以我想你可以使用 score你的实例化类的属性,它是 Int (您使用“score”作为两个变量的名称这一事实令人困惑。我建议更改名称以使它们更明确。)。

@IBAction func leaderboard(sender: UIButton) {
saveHighscore(score.score)
showLeader()
}

关于swift - 如何快速将GameCenter放到应用程序上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32167180/

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