Cocos2d-x游戏开发之CCUserDefault类存储详解

本站文章转载务必在明显处注明:原文接: http://blog.csdn.net/cjsen/article/details/9059257

前言

CCUserDefalt作为NSUserDefalt类的cocos2d-x实现版本,承担了cocos2d-x引擎的记录实现功能。这个是cocos2d-x自带的存储类,比较适合小的数据记录(如用户名,密码等),如果是数据量较大的话还是用SQLite数据库 存储。为了移植性,还是尽量用cocos2d-x的自带功能。

说明

CCUserDefalt的使用过程非常简单,其API,源码也很简单明了

    bool    getBoolForKey(const char* pKey);
    bool    getBoolForKey(const char* pKey, bool defaultValue);
    /**
    @brief Get integer value by key, if the key doesn't exist, a default value will return.
     You can set the default value, or it is 0.
    */
    int     getIntegerForKey(const char* pKey);
    int     getIntegerForKey(const char* pKey, int defaultValue);
    /**
    @brief Get float value by key, if the key doesn't exist, a default value will return.
     You can set the default value, or it is 0.0f.
    */
    float    getFloatForKey(const char* pKey);
    float    getFloatForKey(const char* pKey, float defaultValue);
    /**
    @brief Get double value by key, if the key doesn't exist, a default value will return.
     You can set the default value, or it is 0.0.
    */
    double  getDoubleForKey(const char* pKey);
    double  getDoubleForKey(const char* pKey, double defaultValue);
    /**
    @brief Get string value by key, if the key doesn't exist, a default value will return.
    You can set the default value, or it is "".
    */
    std::string getStringForKey(const char* pKey);
    std::string getStringForKey(const char* pKey, const std::string & defaultValue);
    // set value methods
    /**
    @brief Set bool value by key.
    */
    void    setBoolForKey(const char* pKey, bool value);
    /**
    @brief Set integer value by key.
    */
    void    setIntegerForKey(const char* pKey, int value);
    /**
    @brief Set float value by key.
    */
    void    setFloatForKey(const char* pKey, float value);
    /**
    @brief Set double value by key.
    */
    void    setDoubleForKey(const char* pKey, double value);
    /**
    @brief Set string value by key.
    */
    void    setStringForKey(const char* pKey, const std::string & value);
    /**
     @brief Save content to xml file
     */
    void    flush();
    static CCUserDefault* sharedUserDefault();
    static void purgeSharedUserDefault();
    const static std::string& getXMLFilePath();
    static bool isXMLFileExist();
就是一些设置bool,string,double的键值对,key-value值

一个静态单例方法,获取XML路径,是否已存此文件。

其中staticbool isXMLFileExist();方法在cocos2d-x 2.0以前版本是私有的,现在公开了^_^...

简单的set使用方法

void CCUserDefaultManage::setLoginPassword(const char* pPassword){
    m_password = pPassword;
    CCUserDefault *userDefault = CCUserDefault::sharedUserDefault();
    userDefault->setStringForKey("LoginPassword", pPassword);
    userDefault->flush();
};
其中在设置数据之后记得调用userDefult->flush(); 相当于设置完数据的提交过程

get 方法就简单了

const char* CCUserDefaultManage::getLoginPassword(void){
    if (m_password == NULL) {
        CCUserDefault *userDefault = CCUserDefault::sharedUserDefault();
        std::string password = userDefault->getStringForKey("LoginPassword");
        m_password = password.c_str();
    }
    return m_password;
};

最后我们看看他的源码实现过程是用了系统的 NSUserDefaults(在iOS上)

void CCUserDefault::setStringForKey(const char* pKey, const std::string & value)
{
#ifdef KEEP_COMPATABILITY
    deleteNodeByKey(pKey);
#endif
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithUTF8String:value.c_str()] forKey:[NSString stringWithUTF8String:pKey]];
}
只是针对平台封装了。。

下面给出自己做的登录界面的Demo源码,界面好难看,嘻嘻。。。

 源码

猜你喜欢

转载自blog.csdn.net/CJsen/article/details/9059257
今日推荐