Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 refresh 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...