数据Cocos2d-x常用功能-Cocos2d-x常用工具:计时器、数据读写、文件读写(共6部分)

第三阶段:常用功能5

1.Cocos2d-x计时器
每一帧执行的时候执行一次
#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
   
   
private :
    cocos2d::LabelTTF *label;
   
public :
   
// there's no 'id' in cpp, so we recommend returning the class instance pointer
   
static cocos2d::Scene* createScene();

   
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
   
virtual bool init(); 
   
   
// a selector callback
   
void menuCloseCallback(Object* pSender);
   
   
// implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
   
   
virtual void update( float dt);
   
    void timerHandler(float dt);
};

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
   
// 'scene' is an autorelease object
   
auto scene = Scene::create();
   
   
// 'layer' is an autorelease object
   
auto layer = HelloWorld::create();

   
// add layer as a child to scene
    scene->addChild(layer);

   
// return the scene
   
return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
   
//
   
// 1. super init first
   
if ( !Layer::init() )
    {
       
return false ;
    }
   
    Size visibleSize = Director::getInstance()->getVisibleSize();
   
    label = LabelTTF::create(
"jikexueyuan" , "Courier" , 30 );
    addChild(label);
   
    schedule(schedule_selector(HelloWorld::timerHandler),
1 );
   
//    scheduleUpdate();
   
return true ;
}

void HelloWorld::timerHandler( float dt){
    log(
">>>>>" );
}


void HelloWorld::update( float dt){
   
    label->setPosition(label->getPosition()+Point(
1 , 1 ));
   
   
if (label->getPositionX()> 500 ) {
        unscheduleUpdate();
    }
}


void HelloWorld::menuCloseCallback(Object* pSender)
{
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit( 0 );
#endif
}

2.Cocos2d-x首选项数据读写
会在本地生成一个文件,就算应用程序关闭掉 下一次打开的时候还可以访问到这个数据
//    UserDefault::getInstance()->setStringForKey("data", "Hello jikexueyuan");
   
    //如果访问不到 data 就会输出默认值 Hello World
    log( "%s" ,UserDefault::getInstance()->getStringForKey( "data" , "Hello World" ).c_str());
    
3.Cocos2d-x文件读写
iphone或者ipad模拟器运行   文件的路径
/Users/niezhao/Library/Developer/CoreSimulator/Devices/DB075D5C-5BFB-4DB7-B908-54F321B519CB/data/Containers/Data/Application/F890EE4B-A397-4020-BEC1-49B55E49D850/Documents/
MAC模拟器运行   文件的路径 /Users/niezhao/Documents/
    auto fu = FileUtils::getInstance();
//写出
//    FILE *f = fopen(fu->fullPathFromRelativeFile("data.txt", fu->getWritablePath()).c_str(), "w");
//    fprintf(f, "Hello jikexueyuan\n");
//    fclose(f);
  //读取  
    Data d = fu->getDataFromFile(fu->fullPathFromRelativeFile( "data.txt" , fu->getWritablePath()));
    //打印内容
    log( "%s" ,d.getBytes());
   
   
   
//    log("%s",fu->getWritablePath().c_str());

4.Cocos2d-x的plist文件
创建plist文件:New File—resource—Property List  实质是一个XML文件
    FileUtils *fu = FileUtils::getInstance();
// 如果根节点是dictionary  
    ValueMap vm = fu->getValueMapFromFile("data.plist");
//如果根节点是array就要用 ValueMap vm = fu->getValueVectorFromFile("data.plist");

    log("%s",vm["name"].asString().c_str()); //也可以用 vm.at() 这里 ()中传一个key 
5.Cocos2d-x的xml数据操作
创建xml文件:New File—other—Empty 输入 文件名.xml
<data>
   
<p name = "ZhangSan" age = "10" />
   
<p name = "LiSi" age = "11" />
</data>

#include <tinyxml2/tinyxml2.h>
#include
<json/reader.h>
#include <json/document.h>
//创建文档
    auto doc = new tinyxml2::XMLDocument();
/解析
    doc->Parse(FileUtils::getInstance()->getStringFromFile( "data.xml" ).c_str());
//获取根节点
    auto root = doc->RootElement();
 //遍历全部子对象       for循环的判断条件 e!=NULL可以写成e
    for ( auto e = root->FirstChildElement(); e; e=e->NextSiblingElement()) {
       
        std::string str;
       //遍历当前子对象的所有属性
        for ( auto attr = e->FirstAttribute(); attr; attr=attr->Next()) {
            str+=attr->Name();
            str+=
":" ;
            str+=attr->Value();
            str+=
"," ;
        }
       
        log(
"%s" ,str.c_str());
    }
 sibling /'sɪblɪŋ/ n. 兄弟姊妹;民族成员 sibling nodes 同级节点   
6.Cocos2d-x的json数据操作
创建json文件:New File—other—Empty 输入 文件名.json

[{"name":"ZhangSan","age":20},{"name":"LiSi","age":19}]

#include <json/rapidjson.h>
#include <json/document.h>
    rapidjson::Document d;
//0代表默认的解析方式
    d.Parse< 0 >(FileUtils::getInstance()->getStringFromFile( "data.json" ).c_str());
   //强制转换成int类型    0为索引 name为属性    .GetString()字符串
    log("%s",d[(int)0]["name"].GetString());
数组用[]   对象{} 对象之间用逗号隔开

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值