加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx-文件读写操作(txt、plist、xml、json)(五)

发布时间:2020-12-14 20:14:54 所属栏目:百科 来源:网络整理
导读:1.用户首选项数据 //用户首选项数据 //存入数据 UserDefault::getInstance()-setStringForKey("huangpu","Hello Huangpu!"); //读取数据 log("%s",UserDefault::getInstance()-getStringForKey("huangpu","hello world").c_str()); 2.计时器操作 (1)schedu

1.用户首选项数据

    //用户首选项数据
    //存入数据
    UserDefault::getInstance()->setStringForKey("huangpu","Hello Huangpu!");
    //读取数据
    log("%s",UserDefault::getInstance()->getStringForKey("huangpu","hello world").c_str());

2.计时器操作

(1)scheduleUpdate()和unscheduleUpdate()

使用这种方法只能开始结束,可控性不强

具体如下:1.重写update()方法

HelloWorldScene.h文件

    virtual void update(float dt);

HelloWorldScene.cpp文件

在init()方法中开始更新

    label = LabelTTF::create("haha","Courier",30);
    addChild(label);
    scheduleUpdate();
update()方法实现
//scheduleUpdate()使用方法
void HelloWorld::update(float dt){
    
    label->setPosition(label->getPosition()+Point(1,1));
    if (label->getPositionX()>400) {
//调用此方法停止
       unscheduleUpdate();
    }
}
(2)schedule()方法

使用此方法控制性比较强

HelloWorldScene.h文件中声明处理方法

    void timerHandler(float dt);
HelloWorldScene.cpp文件中实现
void HelloWorld::timerHandler(float dt){
    log(">>>>>>>>>>");
}
在init()方法中调用
    //schedule
    schedule(schedule_selector(HelloWorld::timerHandler),1);

3.TXT文件读写

    //文件读写
    //1.得到实例
    auto fu = FileUtils::getInstance();
    //写文件
    FILE *f = fopen(fu->fullPathFromRelativeFile("data.txt",fu->getWritablePath()).c_str(),"w");
    fprintf(f,"Hello huangpu");
    fclose(f);
    
    //读文件
    Data d = fu->getDataFromFile(fu->fullPathFromRelativeFile("data.txt",fu->getWritablePath()));
    log("%s",d.getBytes());
    log("%s",fu->getWritablePath().c_str());

4.plist文件读取

data.plist内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>haha</key>
	<array>
		<string>afdaf</string>
		<string>helo1</string>
	</array>
	<key>name</key>
	<string>huangpugang</string>
</dict>
</plist>
读取操作
    //plist文件读取
    FileUtils *fu = FileUtils::getInstance();
    ValueMap vm = fu->getValueMapFromFile("data.plist");
    log("%s",vm["name"].asString().c_str());

5.xml文件读取

data.xml中的内容

<data>
    <p name="zhangsan" age="haha"/>
    <p name="lisi" age="xixi"/>
    
</data>

首先导入头文件

#include <tinyxml2/tinyxml2.h>

接着开始读取
//    xml文件读取 头文件 <tinyxml2/tinyxml2.h> 有点问题
    auto doc = new tinyxml2::XMLDocument();
    doc->Parse(FileUtils::getInstance()->getStringFromFile("data.xml").c_str());
    auto root = doc->RootElement();
    for (auto e=root->FirstChildElement();e ; e->NextSiblingElement()) {
        std::string str ;
        for (auto attr=e->FirstAttribute(); attr; attr->Next()) {
            str+=attr->Name();
            str+=":";
            str+=attr->Value();
            str+=",";
        }
        log("%s",str.c_str());
        
    }

6.json文件读取

data.json中的内容

[{"name":"zhangsan","age":20},{"name":"lisi","age":30}]
首先导入头文件
#include <json/document.h>

读取操作

    //json文件读取
    rapidjson::Document d;
    //0表示默认解析方式
    d.Parse<0>(FileUtils::getInstance()->getStringFromFile("data.json").c_str());
    log("%s",d[(int)0]["name"].GetString());
今天就学到这里

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读