数据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);

void update( float dt);
void timerHandler(float dt);
};

"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),216)"> 1
);

// scheduleUpdate();
true ;
}

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


void HelloWorld::update( float dt){

label->setPosition(label->getPosition()+Point(
1 ,216)">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" ,27)">"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( // 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" />
"LiSi" "11" />
</data>

<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;
//遍历当前子对象的所有属性
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}]

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

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...