Jennifer
Jennifer

Reputation: 89

Loading an image from plist

im working on a game where, every "stage" id like to load an image that comes from a plist. This is my plist enter image description here

  NSDictionary *config=[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:
                                                                   @"Levels" ofType:@"plist"]];


  NSDictionary *PicData = config[@"levels"];
    NSString *images = [PicData objectForKey:@"playerIcon"];

However, I am getting an error on the last line when run. Not exactly sure why since I am saying spritenode With image named Images which you get from the dictionary. Thank you, any help would be appreicated

Upvotes: 0

Views: 139

Answers (2)

rmaddy
rmaddy

Reputation: 318934

It seems you don't know how to dig into the proper part of the plist file.

// Get the top-level dictionary
NSDictionary *config=[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Levels" ofType:@"plist"]];

Now you need to get the "levels" array:

NSArray *levels = config[@"levels"];

Now you need to know which level you want. Once you know the level, you can get the dictionary for that level:

NSDictionary *levelData = levels[someLevelNumber];

Now you can get the player icon:

NSString *imageName = levleData[@"playerIcon"];

Upvotes: 1

CodeSmile
CodeSmile

Reputation: 64477

Try this to get the icon for the first level:

NSString *images = PicData[@"levels"][0][@"playerIcon"];

I changed this from the top of my head, not sure if it will throw an error because each accessor returns an object of type 'id' by default. I also changed it to the new indexers which make code like this a lot more readable as you can imagine.

Upvotes: 1

Related Questions