游戏中心身份验证无效

时间:2016-04-01 20:40:39

标签: swift sprite-kit

我的游戏中心身份验证无效。当我构建并运行时,它将不会显示我的用户名..已登录。此外,当我尝试添加我的分数时,我会看到一个屏幕上显示“无数据可用”。这是我的代码。

 override func viewDidLoad() {
        super.viewDidLoad()
        gcAuthPlayer()
        }

@IBAction func GCButton(sender: AnyObject) {



        saveHighScore(GameScene().highScoreNumer)
        showLeaderBoard()
        if GameScene().currentScore > GameScene().highScoreNumer{
            saveHighScore(GameScene().currentScore)
        }


    }
    func showLeaderBoard(){
        let viewController = self.view.window?.rootViewController
        let gcvc = GKGameCenterViewController()

        gcvc.gameCenterDelegate = self

        viewController?.presentViewController(gcvc, animated: true, completion: nil)

    }
    func saveHighScore(number: Int){

        if GKLocalPlayer.localPlayer().authenticated{
            let scoreReporter = GKScore(leaderboardIdentifier: "myleaderboard")

            scoreReporter.value = Int64(number)

            let scoreArray : [GKScore] = [scoreReporter]

            GKScore.reportScores(scoreArray, withCompletionHandler: nil)


        }
    }
    func gcAuthPlayer(){
        let localPlayer = GKLocalPlayer.localPlayer()

        localPlayer.authenticateHandler = {
            (view, error) in


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

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

            }

        }



    }

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

1 个答案:

答案 0 :(得分:0)

这段代码毫无意义

'This is a global variable, prevents endless loops in worksheet change.
Dim running As Boolean

'When any change is made on the worksheet, sends target as the changed cell
Private Sub Worksheet_Change(ByVal Target As Range)
    'Checks to see if a worksheet change is already running, if it is, then it quits 
    If running = True Then Exit Sub
    'We are running code now, so prevent endless loops
    running = True
    Dim YourFirstColumn, YourOtherColumn As Integer

    'Set to your values
    YourFirstColumn = 1
    YourOtherColumn = 2

    'If this is a column you want to change
    If Target.Column = YourFirstColumn Then

        'Change values
        If Target.Value = "In Progress" Then
            Target.Offset(0, Abs(YourFirstColumn - YourOtherColumn)).Value = "50%"
        ElseIf Target.Value = "Closed" Then
            Target.Offset(0, Abs(YourFirstColumn - YourOtherColumn)).Value = "100%"
        End If

    'If this is the other column you want to change
    ElseIf Target.Column = YourOtherColumn Then

        'Change values
        If Target.Value = 0.5 Then
            Target.Offset(0, -Abs(YourFirstColumn - YourOtherColumn)).Value = "In Progress"
        ElseIf Target.Value = 1 Then
            Target.Offset(0, -Abs(YourFirstColumn - YourOtherColumn)).Value = "Closed"
        End If
    End If
    running = false
End Sub

每次尝试更新分数时,您都会创建一个新的GameScene实例,因此您的分数为零

我需要查看更多代码,但现在您需要更改游戏场景中的得分属性。例如,将它设为静态属性,以便在其他类中获取它。

         saveHighScore(GameScene().highScoreNumer)
    showLeaderBoard()
    if GameScene().currentScore > GameScene().highScoreNumer{
        saveHighScor

在场景或ViewController中你可以像这样得到它

class GameScene: SKScene {

       static var currentScore = 0
       static var highscoreNumber = 0
  }

请记住,每次重新启动gameScene时都必须将分数重置为0,因为它是静态属性。

 GameScene.currentScore = 5
 GameScene.highscoreNumber = 5

发布得分的代码应该是这样的

GameScene.currentScore = 0
GameScene.highscoreNumber = 0

您的得分报告代码也应该处理错误并实际执行完成处理程序。所以把它改成这样的东西。

    saveHighScore(GameScene.highScoreNumer)
    showLeaderBoard()
    if GameScene.currentScore > GameScene.highScoreNumer{
        saveHighScor

将代码移动到另一个类中以保持整体代码更清晰,更可重用也是一个好主意。 有关一个简单的例子,请在gitHub上检查这个帮助器。

https://github.com/jackcook/GCHelper

让我知道它是怎么回事。