O'Reilly logo

Cocos2d-x by Example : Beginner's Guide - Second Edition by Roger Engelbert

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required