游戏中心不正常工作。 Swift,Sprite Kit

时间:2015-05-14 21:35:33

标签: swift

我最近尝试将Game Center添加到我的Sprite Kit游戏中,但它无法正常工作。

当游戏在模拟器中启动时,游戏中心登录页面会显示。当我在手机上启动游戏时,它没有。有人能告诉我我做错了什么。

//GameViewController.Swift
import GameKit

class GameViewController: UIViewController, ADBannerViewDelegate, GKGameCenterControllerDelegate {

var bannerView:ADBannerView?

override func viewDidLoad() {
    super.viewDidLoad()

    // Presenting scene without using GameScene.sks
    let skView = view as! SKView
    let myScene = GameScene(size: skView.frame.size)
    myScene.scaleMode = .ResizeFill

    skView.presentScene(myScene)
    authenticateLocalPlayer()

}

//initiate gamecenter
func authenticateLocalPlayer(){

    var localPlayer = GKLocalPlayer.localPlayer()

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

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

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

}

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



//GameScene.Swift
import GameKit

class GameScene: SKScene, SKPhysicsContactDelegate{
var playerScore = 0
func playerScoreUpdate() {
    playerScorelabel.text = "\(playerScore)"
}

func saveHighScore(high:Int) {
    NSUserDefaults.standardUserDefaults().setInteger(high, forKey: "highscore")
    //check if user is signed in
    if GKLocalPlayer.localPlayer().authenticated {

        var scoreReporter = GKScore(leaderboardIdentifier: "TF1G002ID") //leaderboard id here

        scoreReporter.value = Int64(playerScore) //score variable here (same as above)

        var scoreArray: [GKScore] = [scoreReporter]

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

    }
}



//GameOver.Swift
import GameKit

class GameOverScene: SKScene, GKGameCenterControllerDelegate {

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

// Press Finger
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let node = self.nodeAtPoint(location)

        if node.name == "replay" {
            playSound(sound)
        }

        if node.name == "leaderboard" {
            showLeader()
        } 
    }
}

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

}

1 个答案:

答案 0 :(得分:0)

您是否将GameKit框架导入到您的应用中? 转到您的项目 - &gt;一般 - &gt;滚动,直到看到链接的框架 - &gt;点击加号 - &gt;通过搜索

添加GameKit框架

enter image description here