Book Image

Cocos2d-X Game Development Blueprints

By : Karan Sequeira
Book Image

Cocos2d-X Game Development Blueprints

By: Karan Sequeira

Overview of this book

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

Saving a high score using LocalStorage


For a game like this where users are constantly driven to improve their score, it goes without saying that you need to store the user's best score persistently. Even though this game is running in a browser, we can still store data persistently. To accomplish this, we make use of HTML5 LocalStorage. Cocos2d-html5 provides a wrapper, although it is just as easy accessing LocalStorage with the window.localStorage.setItem or window.localStorage.getItem command.

HTML5 LocalStorage stores data in key/value pairs and a web page can only access data stored by itself. Thus our game's data is safe with the browser. This data has no expiry date and will not be destroyed even if the browser is closed. The only exception is if the user chooses to clear the browser's cache. Data is not persistent if the user chooses to browse in private or incognito mode.

We shall store our high-score data with a key specified by the HIGHSCORE_KEY variable defined at the top of gameworld...