在做游戏的过程中基础的数据存储是必须丶,而最基础的无疑是存储游戏的得分多少,就是简简单单的存储一个游戏的得分。
那么如何去存储呢?在cocos2d-x3.0中我们可以使用UserDefault来实现简单的存储得分。
那么可以看一下源代码
<pre name="code" class="cpp">//get类的函数表示得到的值
bool getBoolForKey(const char* pKey); bool getBoolForKey(const char* pKey, bool defaultValue); int getIntegerForKey(const char* pKey); int getIntegerForKey(const char* pKey, int defaultValue); float getFloatForKey(const char* pKey); float getFloatForKey(const char* pKey, float defaultValue); double getDoubleForKey(const char* pKey); double getDoubleForKey(const char* pKey, double defaultValue); std::string getStringForKey(const char* pKey); std::string getStringForKey(const char* pKey, const std::string & defaultValue); Data getDataForKey(const char* pKey); Data getDataForKey(const char* pKey, const Data& defaultValue);
//set类的函数表示设置值为多少
void setBoolForKey(const char* pKey, bool value);
void setIntegerForKey(const char* pKey, int value);
void setFloatForKey(const char* pKey, float value);
void setDoubleForKey(const char* pKey, double value);
void setStringForKey(const char* pKey, const std::string & value);
void setDataForKey(const char* pKey, const Data& value);
<pre name="code" class="cpp">//由此可以得知我们可以通过唯一的标识来存储每一个数据。
//而它的底层是通过用XML自定义标签来保存数据的大体如下
<?xml version="1.0" encoding="UTF-8"?>
<userDefaultRoot>
<mydate>1223</mydate>
</userDefaultRoot>
大体如上所示如果说大家想存储比较多的数据,可以采用SQLite来存储。
<a target=_blank href="http://cn.cocos2d-x.org/tutorial/show?id=1723">详情查看链接</a>
</pre><pre name="code" class="cpp">