As mentioned earlier, PLIST is a Mac- and iOS-specific format to store and retrieve data, so it cannot be used for cross-platform game development. It is very easy to create and understand as compared to other forms, such as XML and JSON.
To load a PLIST file, we obviously need a file to work with. In the project file, there are two files: scene1.plist
and whackamole_template.plist
. Import these files into the project by creating a separate group.
For PLIST, no additional libraries or files are required to import.
A PLIST file is structured similarly to the following:
{ nodes = ( { type = spriteFile; file = "cactus1_00.png"; position = { x = 100; y = 100; }; scale = 1; }, { type = spriteFile; file = "cactus2_00.png"; position = { x = 246; y = 262; }; scale = 1; }, { type = spriteFile; file = "cactus3_00.png"; position = { x = 342; y = 124; }; scale = 1; }, { type = spriteFile; file = "cactus4_00.png"; position = { x = 100; y = 200; }; scale = 1; }, ); }
When you click on the file in the project, you will see that it is displayed in a different format. However, it is a lot easier to add and delete data from the file in this format as you don't have to worry about curly brackets, commas, and other formatting.
Similar to what we did last time, we will pass in the name of the file for which the path would have to be retrieved. After getting the path, the data will be stored in a dictionary. This dictionary, with all the data files, will then be processed to output the data to the console.
To import the JSON file, we will add the following in the init function after the code:
//** loading a PLIST file NSString *fileName = @"scene1.plist"; NSDictionary *dict = [NSDictionarydictionaryWithContentsOfFile:getFileFromPath(fileName)]; [selfprocessPLISTMap:dict];
The getFileFromPath
function is similar to the following:
NSString* getFileFromPath( NSString* file ){ NSArray* path = [file componentsSeparatedByString: @"."]; NSString* actualPath = [[NSBundlemainBundle] pathForResource: [path objectAtIndex: 0] ofType: [path objectAtIndex: 1]]; returnactualPath; }
Next, the processPLISTMap
function is as follows:
-(void) processPLISTMap:(NSDictionary*)dict { NSArray *nodes = [dictobjectForKey:@"nodes"]; for (id node in nodes) { if([[node objectForKey:@"type"] isEqualToString:@"spriteFile"]){ NSLog(@"found spriteFile"); [selfprocessSpriteFile:node]; } } }
As before, in the case of JSON, processSpriteFile
will retrieve the data from the node array passed into the function.
3.145.103.154