Game Development Reference
In-Depth Information
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. You can group multiple updates under one flush, or find a spot in your lo-
gic where you can safely flush updates before exiting the app. Nodes come with
Search WWH ::




Custom Search