John
John

Reputation: 23

Randomizing the contents of a plist

I'm new to Sprite Kit and I have 5 levels in my game. I want to randomize my levels in my game to be at any level once the game starts.

I created my levels in plists and I wanted to know if randomizing my plists would do the trick.

How would I get about randomizing my plists?

Upvotes: 1

Views: 50

Answers (2)

Andrey Gordeev
Andrey Gordeev

Reputation: 32529

Assuming your plist is an array of dictionaries:

NSString *pathToPlist = [[NSBundle mainBundle] pathForResource:@"levels" 
                                                        ofType:@"plist"];
NSArray *levels = [NSArray arrayWithContentsOfFile:pathToPlist];
NSDictionary *level = levels[arc4random_uniform(levels.count)];
GameScene *scene = [[GameScene alloc] initWithSize:self.view.bounds.size 
                                         levelData:level];

Upvotes: 1

Kris Gellci
Kris Gellci

Reputation: 9687

I am not sure how large your plist is or how you load it in but I am assuming that it is loaded in as an NSArray of NSDictionaries. You can turn the array into an NSMutableArray and then run some sort of randomization algorithm on it. Here is an example of such an algorithm.

You can then load the levels from the array as you normally would. If you post the format of the plist and how you parse it, I can further help you in coming up with a solution.

Upvotes: 0

Related Questions