http://www.cocos2d-x.org/docs/api-ref/js/v3x/index.html
I tried here to search about UserDefault
. But i found no result.
Can you please guide me how to save game data using javascript?
Thanks
http://www.cocos2d-x.org/docs/api-ref/js/v3x/index.html
I tried here to search about UserDefault
. But i found no result.
Can you please guide me how to save game data using javascript?
Thanks
Can someone help me please?
try this
cc.sys.localStorage.getItem(key);
cc.sys.localStorage.setItem(key, value);
cc.sys.localStorage.removeItem(key);
This is what I use.
saveGameData : function()
{
var file = {};
file.settings = settings;
file.levelStatus = levelStatus;
if (cc.sys.isNative)
{
var path = jsb.fileUtils.getWritablePath();
cc.log('saving ' + path);
if (jsb.fileUtils.writeToFile({data:JSON.stringify(file)}, path+levelDataFileName))
{
cc.log('save suc');
}
else
{
cc.log('save FAILED');
}
}
else
{
cc.sys.localStorage.setItem(levelDataFileName, JSON.stringify(file));
}
},
loadGameData: function()
{
if (cc.sys.isNative)
{
cc.log('loading game data');
var path = jsb.fileUtils.getWritablePath();
if (jsb.fileUtils.isFileExist(path + levelDataFileName))
{
var temp = jsb.fileUtils.getValueMapFromFile(path+SS.levelDataFileName);
var file = JSON.parse(temp.data);
if (file)
{
cc.log('game data found ' + file.levelStatus.length);
}
}
else
{
cc.log('no saved game data found');
}
}
else
{
var data = cc.sys.localStorage.getItem(SS.levelDataFileName);
if (data != null) {
var file = JSON.parse(data);
}
}
What is SS
in your coding?
var data = cc.sys.localStorage.getItem(SS.levelDataFileName);
I forgot to remove that from the code. SS is the name of a global variable I use.
Check out these videos we made for saving data in Cocos2d-x
C++ - https://www.youtube.com/watch?v=_FYsuSFKCZU
JavaScript - https://www.youtube.com/watch?v=2A5dVQv1C8o