I’m developing a game that uses a basic points system for shooting enemies, and that score is then converted to a coins value for unlocking extra guns in the game. So when you play, your score goes up and when you die a screen shows you your score, and how many coins you got from that playthrough (score x 10 at the moment).
I’m looking for the best way to get the coins value and display it on another screen, the gun selection screen, the first time you play and from then on the coins you get from one playthrough will add to the total coins you have, the value displayed on the gun selection screen. And if it is easy to do, a way of encrypting the coins value.
This is what I’m currently using, with NSUserDefaults, in the play game view:
NSUserDefaults *coins = [NSUserDefaults standardUserDefaults];
NSNumber *myCoins = [NSNumber numberWithInt:_killCount*9.4];
_myCoinsInt = [myCoins integerValue];
_totalCoinsInt = _myCoinsInt + _totalCoinsInt;
[coins setObject:myCoins forKey:@"coins"];
[coins synchronize];
NSUserDefaults *totalCoins = [NSUserDefaults standardUserDefaults];
NSNumber *theTotalCoins = [NSNumber numberWithInt:_totalCoinsInt];
[totalCoins setObject:theTotalCoins forKey:@"totalCoins"];
[totalCoins synchronize];
NSLog(@"%@", theTotalCoins);
And in the Gun selection screen, under the viewDidLoad method:
NSUserDefaults *totalCoins = [NSUserDefaults standardUserDefaults];
NSInteger _totalCoinsInt = [totalCoins objectForKey:@"totalCoins"];
NSString *intString = [NSString stringWithFormat:@"%d", _totalCoinsInt];
scoreField.text = intString;
I think the simplest way to go would be to store the amount of coins in the
[NSUserDefaults standardUserDefaults]either way, you can do it the way the other answers provided but you’re going to need to save that data when your app closes.I would also back this value up with your server (if you have one) to keep track of different users and to save the values. Check out parse.com if you don’t have a web service already.
Example