cocos2d JS Local Storage (localstorage)

Source: Internet
Author: User

In the game development process, if you can save the game data, the next time you enter the game, the data will still exist; just like the general game save the game progress, the development and testing is very convenient, especially for the client does not server and want to achieve server-side effect of developers; Here is a brief introduction to local data access (Sys.localstorage) in COCOS2DJS development;

/***************************** Writing Data *********************************/
1 sys.localStorage.setItem ("ItemName", string);
The parameter 1 is the name of the field to be written, and the data is read according to the field name, and the parameter 2 must be a string, if the JSON data structure needs to be converted to string json.stringify (Json.json);/****************** Reading data *********************************/
1 sys.localStorage.getItem ("ItemName");
The parameter is the field name at the time of writing//The data read here is a string form, if JSON format is required, the string must be converted to JSON format json.parse (string);/************************** Delete the data * * * * *****************************/
1 sys.localStorage.removeItem ("ItemName");
parameter is the name of the field when writing/***************************** Write json file demo*************************************/
1 varBaselocaldata = {};2 /*3 * Write data, store the data locally4 * @param jsonname:json file name5  */6 varBasedata = [{a:1},{b:2},{c:3},{d:4},{e:5},{f:6},{g:7}];7Baselocaldata.setitem =function(Jsonname)8 {9     varBasedata =json.stringify (jsonname);TenSys.localStorage.setItem ("Basedata", basedata); One }; A /* - * Read basic data - * Return JSON format data the  */ -Baselocaldata.getitem =function() - { -     varBaseData1 = Json.stringify (Basedata);//convert JSON format to string +Sys.localStorage.setItem ("Basedata", baseData1);//Store the data locally -     varBasedataa = Sys.localStorage.getItem ("Basedata");//read data from local +Basedataa = Json.parse (BASEDATAA);//convert a string into JSON A     returnBasedataa; at }; - /* - * Delete Data -  */ -Baselocaldata.deleteitem =function() - { inSys.localStorage.removeItem ("Basedata"); -};

Cocos2d JS Local Storage (localstorage)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.