Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2dx-文件读写操作(txt、plist、xml、json)(五)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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内容:

@H_262_55@<?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中的内容

@H_262_55@<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中的内容

@H_262_55@[{"name":"zhangsan","age":20},{"name":"lisi","age":30}]首先导入头文件 @H_262_55@#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());
今天就学到这里

大佬总结

以上是大佬教程为你收集整理的cocos2dx-文件读写操作(txt、plist、xml、json)(五)全部内容,希望文章能够帮你解决cocos2dx-文件读写操作(txt、plist、xml、json)(五)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。