Я сделал игру, используя SpriteKit и Xcode 7 beta. Я попытался добавить GameCenter и Leaderboard, но проблема в том, что оценка в таблице лидеров не изменится (HighScore не загружается в GameCenter), он остается 0 все время, и я не знаю, как это исправить. Я использую 2 различных файлов: GameViewController.swift и PointsLabel.swiftКак правильно установить GameCenter на приложение?
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' как параметр. – Moritz
Итак, как мне нужно вызвать класс PointsLabel.swift в классе GameViewController.swift, потому что, пока я показываю по вопросу, находится в классе PointsLabel.swift и есть как Int? – Emm