Book Image

Cocos2d Game Development Blueprints

By : Jorge Jordán
Book Image

Cocos2d Game Development Blueprints

By: Jorge Jordán

Overview of this book

Table of Contents (15 chapters)
Cocos2d Game Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 standardUserDefaults...