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)