I'm working on a game and it has 50 levels. I want to unlock each level after completing the level that comes before it. Right now I set up 50 SKSpriteNodes
labeled 1 to 50 so they are numbered levels. I want the NSUserDefaults to tell the level-selection screen that the user has unlocked a new level when the level is complete. (i.e. if user completes level 2, unlock level 3). I was trying to follow the solution on this question How to save NSMutablearray in NSUserDefaults but it wasn't working for me because I wasn't sure how to have an NSMutableArray
that would be in both the level selection menu and the current level itself.
This is the bit of code I had tried:
[[NSUserDefaults standardUserDefaults] setObject:levelAchieved forKey:@"level2"];
[[NSUserDefaults standardUserDefaults] synchronize];
I used this when the user finished level 1
Then I tried calling this:
NSMutableArray *levelAchieved = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"level2"] mutableCopy];
To tell the level-selection scene that the user had achieved a new level and to unlock it on that scene.
But this didn't work because I wasn't able to have the array in both the scenes, even when I made sure I imported the header file that had the NSMutableArray *levelAchieved
in.
How can I properly add to the array, but furthermore --> Is this the right way to be approaching this type of thing? Is there a different way to unlock a series of levels between 2 scenes?