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 – storing the completed levels


Open the LevelSelectLayer class.

  1. This is how the number of levels completed is retrieved from inside the layer's constructor:

    _levelsCompleted = UserDefault::getInstance()- >getIntegerForKey("levelsCompleted");
  2. Initially, _levelsCompleted will equal 0 if no data is present. So we store level 1 as "unlocked". This is how that's done:

    if (_levelsCompleted == 0) {
        _levelsCompleted = 1;
        UserDefault::getInstance()->setIntegerForKey("levelsCompleted", 1);
        UserDefault::getInstance()->flush();
    }
  3. Then, whenever we start a new level, we update the number of levels completed if the new level number is larger than the value stored.

    if (_currentLevel > _levelsCompleted) {
        _levelsCompleted = _currentLevel;
        UserDefault::getInstance()->setIntegerForKey("levelsCompleted", _levelsCompleted);
        UserDefault::getInstance()->flush();
    }

    Note

    You don't have to flush the data (using flush) each time you update every single bit in it....