cocos-js,数据本地存储
发布时间:2020-12-14 17:06:02 所属栏目:百科 来源:网络整理
导读:一、前言 在cocos-js 3.0以上的版本中,当我们用到本地存储的时候,发现以前用到的UserDefault在JS中并没有导出,而是换成了LocalStorage。 二、基本使用 /** Initializes the database. If path is null,it will create an in-memory DB. */void CC_DLL loc
一、前言在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(); 在
三、重点注意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数据库的方法实现本地存储。 四、模仿UserDefault实现数据封装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)) },} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |