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

前端 未结 2 1461
伪装坚强ぢ
伪装坚强ぢ 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:45

    yes, you have different options to save data : nsuserdefaults, write to a file and store the file, coredata, or use a webserver with a database.

    If someone knows how to hack your app, he will use what is inside your app (the 3 first methods), this is why many big games use internet (webserver) to check against what was bought by the user.

    But, it is not so often that someone hack your game, if he does though, he would never pay anyway. So stay with nsuserdefaults, it is simple and good enough in my opinion.

    0 讨论(0)
  • 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 )
    
    0 讨论(0)
提交回复
热议问题