I have two labels, one that shows current level another that shows high level. The current level label is updated each time you beat the computer.
current_level_value++;
For the high level lable I want to do a check to see if the current level labels value is larger than the high level label value, and if so save and update the high level label value. Anb also do this when the user clickes the home button.
I'm not getting any errors or crashes, the current level label value is updating just fine, but when I quit and reopen the app the high level label value is still at zero.
Any thoughts?
Thanks
Quote:
Originally Posted by smasher
It's probably better to start a new thread if you have a new question. Your "if" statement is bad - current_level and high_level are pointers (memory addresses,) so comparing them will not give you what you want. You should be comparing something else instead, like [current_level.text intValue] > [high_level.text intValue] .
Also it looks like you're doing the loading and saving in
viewDidLoad? Don't you need to save when the game is over, not when it's starting?
Thanks Smasher,
I was able to do the check, current level to high level, and it updates high level after the level ends but only durring your game session.
I used this code to do that.
Code:
//COMPARE CURRENT LEVEL WITH HIGH LEVEL
if([current_level.text intValue] > [high_level.text intValue]){
high_level.text = current_level.text;
}
But i cant seem to get the high level to save and load in when you quit and restart.
I tried to place this in the view did load, and nothing.
Code:
//SAVE HIGH LEVEL
//[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
//[[NSUserDefaults standardUserDefaults] synchronize];
And i cant seem to add it to my AppDelegate.m in the code below, because "high_level_value" isnt being implemented ther, its in my ViewController.m
Do you have a pointer to your controller in your appdelegate? Then you can put the "save score" stuff in a method in the controller, and call [myController saveScore] from applicationWillTerminate.
If not, you can register your controller to get applicationWillTerminate messages with code like this in viewDidLoad:
Do you have a pointer to your controller in your appdelegate? Then you can put the "save score" stuff in a method in the controller, and call [myController saveScore] from applicationWillTerminate.
If not, you can register your controller to get applicationWillTerminate messages with code like this in viewDidLoad:
- (void)viewDidLoad
{
//Load data here
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:app];
[super viewDidLoad];
}
- (void)applicationWillTerminate:(NSNotification *)notification
{
//Save data here
}
Hope that helps.
I tried this and I don't get any errors or crashes, but it doesnt seem to save the high score label and load it back in when quit and reopened. What am i doing wrong here?
Code:
- (void)viewDidLoad
{
//Load data here
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"]) {
high_level_value=[[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"];
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:app];
[super viewDidLoad];
}
- (void)applicationWillTerminate:(NSNotification *)notification
{
//Save data here
[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Just tried that and got nothing, no errors, or crashes, just not loading in the high level when i restart. Just a question, this should work in the sim right, its not something that must be on the device?
//SAVE HIGH LEVEL
[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
With this
Code:
//SAVE HIGH LEVEL
[[NSUserDefaults standardUserDefaults] setInteger:[high_level_value intValue] forKey:@"high_score"];
Hope this helps!
__________________
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” Anonymous
//SAVE HIGH LEVEL
[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
With this
Code:
//SAVE HIGH LEVEL
[[NSUserDefaults standardUserDefaults] setInteger:[high_level_value intValue] forKey:@"high_score"];
Hope this helps!
When i do that i get this warning: invalid receiver type 'NSInteger'
Also and now im really stumped, when the player gets hit by the enemy, or when you collect the energy the value for the high level jumps from 0 to 264...
And this only seems to be happening in the simulator, not on the device.
I dont get any warnings, but still not savint the high level value.
I am doing this in the game loop,
Code:
//COMPARE CURRENT LEVEL WITH HIGH LEVEL.
if([current_level.text intValue] > [high_level.text intValue]){
high_level.text = current_level.text;
}
And that is working fine, as you play the game its setting the high level to the current level if the current level is greater than the high level.
So im assuming that when the game is closed, the high level value should be saving with this.
Code:
- (void)applicationWillTerminate:(NSNotification *)notification
{
//Save data here
[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"app quit, save high level");
}
And loading with this when restarting
Code:
- (void)viewDidLoad {
//Load data here
high_level_value=[[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"];
NSLog(@"set high level");
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
NSLog(@"check close high level");
[super viewDidLoad];
}
- (void)viewDidLoad {
self.prefs = [NSUserDefaults standardUserDefaults];
// Check to see what was stored in the user prefs, and update the label with that colour.
if ([prefs stringForKey:@"high_score"]) {
NSLog(@"The prefs is set.");
high_level.text = [prefs stringForKey:@"high_score"];
}
high_level_value=[[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"];
NSLog(@"set high level");
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
NSLog(@"check close high level");
[super viewDidLoad];
}
- (void)applicationWillTerminate:(NSNotification *)notification
{
//Save data here
[[NSUserDefaults standardUserDefaults] setInteger:[high_level.text intValue] forKey:@"high_score"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"app quit, save high level");
}