第三阶段:常用功能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);
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
}
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");
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/
auto
fu = FileUtils::getInstance();
//写出
// FILE *f = fopen(fu->fullPathFromRelativeFile("data.txt",fu->getWritablePath()).c_str(),"w");
// fprintf(f,"Hello jikexueyuan\n");
// 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文件
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数据操作
<data>
<p name = "ZhangSan" age "10" />
"LiSi" "11" />
<p name = "ZhangSan" age "10" />
"LiSi" "11" />
</data>
<tinyxml2/tinyxml2.h>
#include <json/reader.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;
std::string str;
//遍历当前子对象的所有属性
auto
attr = e->FirstAttribute(); attr; attr=attr->Next()) {
str+=attr->Name();
str+= ":" ;
str+=attr->Value();
str+= "," ;
}
log( "%s" ,str.c_str());
}
str+=attr->Name();
str+= ":" ;
str+=attr->Value();
str+= "," ;
}
log( "%s" ,str.c_str());
}
sibling
/'sɪblɪŋ/
n. 兄弟姊妹;民族成员
sibling nodes
同级节点
6.Cocos2d-x的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());
log(int)0]["name"].GetString());
数组用[] 对象{} 对象之间用逗号隔开
原文链接:https://www.f2er.com/cocos2dx/345716.html