Time for action – storing the completed levels
Open the LevelSelectLayer
class.
- This is how the number of levels completed is retrieved from inside the layer's constructor:
_levelsCompleted = UserDefault::getInstance()- >getIntegerForKey("levelsCompleted");
- Initially,
_levelsCompleted
will equal0
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(); }
- 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...