Game Development Reference
In-Depth Information
Time for action - retrieving data from the
.plist file
The level data is loaded in GameLayer .
1. Inside the GameLayer constructor, we load the data like this:
_levels = FileUtils::getInstance()-
>getValueVectorFromFile("levels.plist");
Cocos2d-x will take care of mapping FileUtils to the correct target. There is
FileUtils for each platform that is supported by the framework and they all can
be made to work with the .plist format. Sweet! If the data in the .plist file is
an Array , you must convert it to ValueVector ; if it's Dictionary , you must
convert it to a ValueMap . We'll do that next when we load the data for a specific
level.
Note
If we divide the levels into multiple .plist files, then we would need logic to re-
fresh the _levels array each time a new .plist file is loaded.
2. Inside the loadLevel method, we load the data for the level like this:
ValueMap levelData =
_levels.at(_currentLevel).asValueMap();
Here, the data in the .plist file is Dictionary , so we must convert the data into a
ValueMap .
And that's it for the loading and parsing. Now we can proceed to retrieving data for
our level.
Each level dictionary starts with the data regarding the level's gravity (a level may
start with a different gravity value), the start point where the player should be
placed, and the end point where the igloo should be placed.
3. These values are retrieved like this in our code:
Search WWH ::




Custom Search