如何使用Cocos2D-JS将变量保存到Android?

zal*_*517 4 javascript android cocos2d-x cocos2d-js

我正在尝试将高分整数保存到用户的Android系统中,以便它可以在所有游戏体验中持续存在.

我已经读过使用Cocos2D-X可以使用,NSUserDefaults但Cocos2D-JS API似乎根本没有.

任何人都有这方面的经验,有没有其他有效的方法来解决这个问题?

Seb*_*ste 16

使用Cocos2D-JS编译本机应用程序时,您只需使用localStorage就像在浏览器中运行游戏一样:D

例如:

//Handle for quick access to Cocos2D's implementation of Local Storage:
var ls = cc.sys.localStorage;

var value = "foo";
var key  = "bar";

//This should save value "foo" on key "bar" on Local Storage
ls.setItem(key, value);

//This should read the content associated with key "bar" from Local Storage:
var data = ls.getItem(key);

cc.log(data); //Should output "foo" to the console.

//This should remove the contents of key "bar" from Local Storage:
ls.removeItem(key);

//This should print "null"
data = ls.getItem(key);
cc.log(data);
Run Code Online (Sandbox Code Playgroud)