O'Reilly logo

Cocos2d Cross-Platform Game Development Cookbook - Second Edition by Siddharth Shekar

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

Using NSUserDefaults

NSUserDefaults is a simple way of storing basic information, such as scores and current level, that is not too heavy on code . All you have to do is provide the value and key while storing the data. Later, when you wish to retrieve the data, all you have to do is provide the key, and the data will be retrieved.

Let's take a look at how to use this.

How to do it…

Let's suppose we want to store the high score value. We will have to do the following:

intcurrentHighScore = 24;

[[NSUserDefaultsstandardUserDefaults] setInteger:currentHighScoreforKey:@"highScore_key"];
[[NSUserDefaultsstandardUserDefaults] synchronize];

It is important to synchronize because otherwise if we shut down the application and reopen it, the data will be lost. ...

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