gpt4 book ai didi

ios - 使用 SpriteKit 的 Swift Gamecenter Leaderboard Done 按钮不起作用

转载 作者:行者123 更新时间:2023-11-28 10:25:48 24 4
gpt4 key购买 nike

我目前正在使用 Swift 开发我的第一个 iOS 游戏。不幸的是,到目前为止,我在实现 Gamecenter 时遇到的问题比其他任何事情都多。

在弄清楚如何弹出排行榜并保存高分后,我遇到了下一个问题:

如何使“完成”按钮起作用?如果我按下它,什么也不会发生。

首先是我的代码:

GameViewController.swift:

import GameKit        

....

override func viewWillLayoutSubviews() {

let skView = self.view as SKView
skView.ignoresSiblingOrder = true

let scene = GameScene.sceneWithSize(skView.bounds.size)
scene.scaleMode = .AspectFill

skView.presentScene(scene)

authenticateLocalPlayer()

}

func authenticateLocalPlayer(){
var localPlayer = GKLocalPlayer()
println(localPlayer)
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if ((viewController) != nil) {
self.presentViewController(viewController, animated: true, completion: nil)
}else{

println((GKLocalPlayer.localPlayer().authenticated))
}
}
}

GameScene.swift:

    import GameKit        

....

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

var touch:UITouch = touches.anyObject() as UITouch
var location:CGPoint = touch.locationInNode(self)

if gameCenterRect.contains(location) {

var vc = self.view?.window?.rootViewController
var gc = GKGameCenterViewController()
vc?.presentViewController(gc, animated: true, completion: nil)

}

func saveHighscore(score:Int){

NSUserDefaults.standardUserDefaults().setObject(score, forKey: "kHighscore")

if GKLocalPlayer.localPlayer().authenticated {
var scoreReporter = GKScore(leaderboardIdentifier: "LEADERBOARD_ID")
scoreReporter.value = Int64(self.highscore)
var scoreArray: [GKScore] = [scoreReporter]
//println("report score \(scoreReporter)")
GKScore.reportScores(scoreArray, {(error : NSError!) -> Void in
if error != nil {
println("error")
//NSLog(error.localizedDescription)
}
})

}
}

我必须在我的代码中的什么地方添加一些东西才能返回?

最佳答案

您必须将此行添加到您的游戏场景中:

class GameScene: SKScene, SKPhysicsContactDelegate,UIGestureRecognizerDelegate,     GKGameCenterControllerDelegate{

func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
{
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)

}

并解决这个问题

 if gameCenterRect.contains(location) {

var vc = self.view?.window?.rootViewController
var gc = GKGameCenterViewController()
gc.gameCenterDelegate = self
vc?.presentViewController(gc, animated: true, completion: nil)

}

关于ios - 使用 SpriteKit 的 Swift Gamecenter Leaderboard Done 按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25976165/

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