如何快速将GameCenter应用于应用程序? [英] How to put GameCenter on application with swift?
问题描述
我使用SpriteKit和Xcode 7 Beta开发了一款游戏.我尝试添加GameCenter和排行榜,但是问题是排行榜中的得分不会改变(HighScore不会上传到GameCenter),它始终保持为0,而且我不知道如何解决.我正在使用2个不同的文件:GameViewController.swift和PointsLabel.swift
I made a game using SpriteKit and Xcode 7 beta. I tried to add GameCenter and Leaderboard but the problem is that the score in the leaderboard won't change (HighScore doesn't upload to GameCenter), It stay 0 all the time and I don't know how to fix it. I'm using 2 different files: GameViewController.swift, and PointsLabel.swift
GameViewController.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)
}
此文件中存在错误:
无法使用类型为((PointsLabel!)''的参数列表调用'saveHighscore'
Cannot invoke 'saveHighscore' with an argument list of type '(PointsLabel!)'
关于代码:
@IBAction func leaderboard(sender: UIButton) {
saveHighscore(score) //<- Here is Error
showLeader()
}
PointsLabel.swift:
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
作为参数.
Your score
variable is of type PointsLabel
but your saveHighscore
function expects an Int
as parameter.
查看您的代码,变量score
将是PointsLabel
的实例,因此我想您可以使用实例化类的score
属性,该属性是Int
(事实上,您使用了分数"作为两个变量的名称是令人困惑的.我建议更改名称以使其更明确.)
Looking at your code, the variable score
will be an instance of PointsLabel
so I guess you could use the score
property of your instanciated class, which is an Int
(the fact that you used "score" as a name for both variables is confusing. I suggest changing names to make them more explicit.).
@IBAction func leaderboard(sender: UIButton) {
saveHighscore(score.score)
showLeader()
}
这篇关于如何快速将GameCenter应用于应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!