Cocos2d-x 3.10版本 UserDefault

 这是一个以XML文件格式来存放简单数据的,可存储数据类型为bool, int, float, double, string。

他的部分源文件为:

std::string getStringForKey(const char* key);
    
   
/**
     * Get string value by key, if the key doesn't exist, will return passed default value.
     * @param key The key to get value.
     * @param defaultValue The default value to return if the key doesn't exist.
     * @return String value of the key.
     * @js NA
     */

    virtual std::string getStringForKey(const char* key, const std::string & defaultValue);
    
    /**
     * Get Data value by key, if the key doesn't exist, will return an empty Data.
     * @param key The key to get value.
     * @return Data value of the key.
     * @js NA
     */

    Data getDataForKey(const char* key);
    
    /**
     * Get Data value by key, if the key doesn't exist, will return an empty Data.
     * @param key The key to get value.
     * @param defaultValue The default value to return if the key doesn't exist.
     * @return Data value of the key.
     * @js NA
     */

    virtual Data getDataForKey(const char* key, const Data& defaultValue);

把数据存入里面,只需相应的api即可设置,取出等:

auto userdefault = UserDefault::getInstance();

//查看路径,测试用
log(userdefault->getXMLFilePath().c_str()); 

//存储本次游戏分数
char score_str[100] = {0};
sprintf(score_str, "%d", m_score);
userdefault->setStringForKey("LastScore", score_str);


//存储最佳游戏分数
auto bestscore = userdefault->getStringForKey("BestScore");
if (m_score > atoi(bestscore.c_str()))
userdefault->setStringForKey("BestScore", score_str);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值