1

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
John
  • 23
  • 2

2 Answers2

1

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];
Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
  • I entered the GameScene line of code but I'm getting the following error: "Property 'size' not found on object of type 'SKView * `MyScene *scene = [[MyScene alloc] initWithSize:self.view.size levelData:level];` – John Nov 05 '14 at 04:13
0

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.

Community
  • 1
  • 1
Kris Gellci
  • 9,539
  • 6
  • 40
  • 47