Storing data using NSUserDefault
The NSUserDefault
class provides an easy way to store user preferences in the default system. It can be used to save some user configurations such as preferred filters in a hotel booking application, sound activation in a game, and it can even be used to keep progress in games.
This class offers methods to store several data types (bool, float, double, integer, URL, and even custom objects) to save whatever the game needs.
Therefore we can implement the goToNextLevel
method, but first of all, we must declare a pair of keys we will use to store the needed data. Add the following lines at the top of GameScene.m
:
#define kTutorialSucceded @"Tutorial_Succeded" #define kCurrentLevel @"Current_Level"
Now add the following lines to implement the method:
-(void)goToNextLevel:(id)sender{ // If we had succeeded the tutorial if((int)_gameState == stateTutorialSuccess) { // Store tutorial success in user default [[NSUserDefaults...