SpriteKit: Why does it wait one round for the score to update? (Swift)

后端 未结 3 548
遇见更好的自我
遇见更好的自我 2020-12-03 16:33

In one scene, I have this code:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(score, forKey: \"scoreKey\")

defaults.synchronize()         


        
3条回答
  •  情话喂你
    2020-12-03 17:10

    To answer the question from your comment about using global structure...

    According to docs :

    Global variables are variables that are defined outside of any function, method, closure, or type context.

    Means you should define your struct right after the import statements at the top of the any file.

    You make a structure like pointed from the link I've posted in comments, like this (I've placed the struct definition inside the GameScene.swift file):

    struct GlobalData
     {
        static var gold  = 0;
        static var coins = 0;
        static var lives = 0;
        static var score = 0;
     }
    

    This struct will be available in GameOverScene as well. So, before transition, you will do something like :

    GlobalData.score = 20
    GlobalData.coins = 10
    //etc.
    

    And in your GameOverScene you will access it like this:

    scoreNode.text = String(GlobalData.score)//scoreNode is SKLabelNode
    

提交回复
热议问题