Reputation: 691
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;
Upvotes: 0
Views: 249
Reputation: 4085
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
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//grab coins
int totalCoins = [defaults integerForKey:@"totalCoins"];
//add coins
totalCoins += coinsThisLevel;
//save
[defaults setInteger:totalCoins forKey:@"totalCoins"];
[defaults synchronize];
Upvotes: 4
Reputation:
Well for the score I would recommend you save it with NSUserDefault and save it as a label. Some Code:
//Saving
[[NSUserDefaults standardUserDefaults] setInteger:HighScore forKey:@"HighScore"];
//Loading
NSInteger highScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScore"];
Check out the documentation: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html
Upvotes: 1
Reputation: 7703
Are you familiar with the MVC (model-view-controller) design pattern? That's what you need here. I'd do something like create a Coins class, create an instance of it in the app delegate and pass that instance around among the various view controllers. You can have methods in your class for adding more coins, saving the coins value, encrypting if necessary. You can use KVO (key-value observing) to watch your coins instance for changes to a @property so interested parties can do things like update their display when the coins amount changes.
Upvotes: 1