In my code ,when the user completes a level, it unlocks the next level (out of a total of 15 levels).
However, I have noticed that, when I complete the level, first time, it works, however, when I go back to it and try again, it crashes with this error;
[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object
The code part is this;
//Unlock Next Level
if (levelNumber != 15) {
[m_appDelegate.levels replaceObjectAtIndex:levelNumber withObject:[NSNumber numberWithBool:NO]];
[m_appDelegate saveLevels];
}
If I remove this line;
[m_appDelegate.levels replaceObjectAtIndex:levelNumber withObject:[NSNumber numberWithBool:NO]];
Then the app does not crash, but of course, it does not unlock any further levels.
Some references below that may help;
int levelNumber;
@property (nonatomic, readwrite) int levelNumber;
- (void) actionLockedLevel:(id)sender {
selectedLevel = ((CCNode*)sender).tag;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Level Locked" message:@"Level is locked. Pass the previous level first." delegate:nil cancelButtonTitle:@"No" otherButtonTitles:nil];
[alert show];
}
- (void) actionUnlockedLevel:(id)sender {
CCScene *scene = [CCScene node];
TimedLevel *layer = [TimedLevel node];
layer.levelNumber = ((CCNode*)sender).tag;
[scene addChild:layer];
[[CCDirector sharedDirector] replaceScene:scene];
}
In AppDelegate.m, levels are called in this;
- (void) loadLevels {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"LEVELS"] == nil) {
levels = [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],nil];
} else {
self.levels = [defaults objectForKey:@"LEVELS"];
}
if ([defaults objectForKey:@"LEVELS_PIZZAS"] == nil) {
levelsPizzas = [[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithInteger:200],
[NSNumber numberWithInteger:250],
[NSNumber numberWithInteger:300],
[NSNumber numberWithInteger:350],
[NSNumber numberWithInteger:400],
[NSNumber numberWithInteger:450],
[NSNumber numberWithInteger:500],
[NSNumber numberWithInteger:550],
[NSNumber numberWithInteger:600],
[NSNumber numberWithInteger:650],
[NSNumber numberWithInteger:700],
[NSNumber numberWithInteger:800],
[NSNumber numberWithInteger:900],
[NSNumber numberWithInteger:1000],
[NSNumber numberWithInteger:1100],nil];
} else {
self.levelsPizzas = [defaults objectForKey:@"LEVELS_PIZZAS"];
}
if ([defaults objectForKey:@"COLLECTED_PIZZAS"] == nil) {
self.nCurClickAmounts = 0;
} else {
self.nCurClickAmounts = [[defaults objectForKey:@"COLLECTED_PIZZAS"] intValue];
}
[defaults synchronize];
}
- (void) saveLevels {
[[NSUserDefaults standardUserDefaults] setObject:levels forKey:@"LEVELS"];
[[NSUserDefaults standardUserDefaults] setObject:levelsPizzas forKey:@"LEVELS_PIZZAS"];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:nCurClickAmounts] forKey:@"COLLECTED_PIZZAS"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Aucun commentaire:
Enregistrer un commentaire