在cocos-Js 3.0以上的版本中,当我们用到本地存储的时候,发现以前用到的UserDefault在Js中并没有导出,而是换成了localstorage。
二、基本使用/** Initializes the database. If path is null,it will create an in-memory DB. */voID CC_DLL localstorageInit( const std::string& fullpath = "");/** Frees the allocated resources. */voID CC_DLL localstorageFree();/** Sets an item in the Js. */voID CC_DLL localstorageSetItem( const std::string& key,const std::string& value);/** Gets an item from the Js. */bool CC_DLL localstorageGetItem( const std::string& key,std::string *outItem );/** Removes an item from the Js. */voID CC_DLL localstorageRemoveItem( const std::string& key );/** Removes all items from the Js. */voID CC_DLL localstorageClear();
在localstorage.h
文件中我们查看了一下源码,使用方法就是设置Key-Value的方式
voID localstorageSetItem( const std::string& key,const std::string& value){ assert( _initialized ); int ok = sqlite3_bind_text(_stmt_update,1,key.c_str(),-1,sqlITE_TRANSIENT); ok |= sqlite3_bind_text(_stmt_update,2,value.c_str(),sqlITE_TRANSIENT); ok |= sqlite3_step(_stmt_update); ok |= sqlite3_reset(_stmt_update); if( ok != sqlITE_OK && ok != sqlITE_DONE) printf("Error in localstorage.setItem()\n");}
在setItem的实现方法中,我们可以看到是使用了sqlite3数据库的方法实现本地存储。
并且,我们只能保存字符串数据,所以如果要保存其他类型的数据的时候,要记得进行数据转换。
var LocalKeyConst = { IS_SHIP_SKIP : "IS_SHIP_SKIP",//战舰自动跳过动画}var LocalManager = { setBool:function(_key,_value){ cc.sys.localstorage.setItem(_key,_value.toString()) },getBool:function(_key){ return cc.sys.localstorage.getItem(_key) == "false" ? false : true },setInt:function(_key,getInt:function(_key){ return Number(cc.sys.localstorage.getItem(_key)) },setString:function(_key,_value) },getString:function(_key){ return cc.sys.localstorage.getItem(_key) },setobject:function(_key,JsON.stringify(_value)) },getobject:function(_key){ return JsON.parse(cc.sys.localstorage.getItem(_key)) },}总结
以上是内存溢出为你收集整理的cocos-js,数据本地存储全部内容,希望文章能够帮你解决cocos-js,数据本地存储所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)