How would I save “coins” in my game in Swift SpriteKit?

前端 未结 2 1467
伪装坚强ぢ
伪装坚强ぢ 2021-01-23 16:39

I have this game where the user collects coins and I want them to be able to save them and use the coins for in app purchaces. How would I do this? Could I use NSUserDefaults fo

2条回答
  •  余生分开走
    2021-01-23 16:57

    Reseting the coinScore

    NSUserDefaults.standardUserDefaults().removeObjectForKey("coinScore")
    

    loading the saved score

    var coinScore =  NSUserDefaults.standardUserDefaults().integerForKey("coinScore")
    
    var coins = 5
    

    updating the coinScore

    if coins > coinScore  {
        coinScore = coins
        NSUserDefaults().setInteger(coinScore, forKey: "coinScore")
    }
    
    println( NSUserDefaults().integerForKey("coinScore").description )
    
    coins += 20
    
    if coins > coinScore  {
        coinScore = coins
        NSUserDefaults().setInteger(coinScore, forKey: "coinScore")
    }
    
    println( NSUserDefaults().integerForKey("coinScore").description )
    

提交回复
热议问题