Cocos2dx3.2以后使用Vector代替了CCArray。案例如下:
头文件:T02Vector.h
#ifndef
__T02Vector_H__
#define
__T02Vector_H__
#include
"T32.h"
class
T02Vector
: public
Layer
{
public:
CREATE_FUNC(T02Vector);
//Cocos2dx3.2以后使用Vector代替了CCArray
Vector
_arr;
bool
init();
};
#endif
编写:T02Vector.cpp
#include
"T02Vector.h"
//in cocos3.2 Vector代替CCArray
//如果不是Ref的子类,那不能用Vector,应该用std::vector
bool
T02Vector::init()
{
Layer::init();
Sprite*
sprite
= Sprite::create();
//增加元素
_arr.pushBack(sprite);
//遍历
Vector::iterator
it;
for
(it
= _arr.begin();
it
!= _arr.end();
++it)
{
Sprite*
s
= *it;
}
for
(auto
it
= _arr.begin();
it
!= _arr.end();++it)
{
}
for
(auto
it:
_arr)
{
}
//从后往前遍历
for
(auto
it
= _arr.rbegin();
it
!= _arr.rend();++it)
{
}
//删除
_arr.eraseObject(sprite);
return
true;
}
2 Map的用法(注意字符编解码的第三方库有:iconv,cocos中集成的有这方面的功能)
头文件:T03Map.h
#ifndef
__T03Map_H__
#define
__T03Map_H__
#include
"T32.h"
class
T03Map :
public
Layer{
public:
CREATE_FUNC(T03Map);
bool
init();
};
#endif
编写:T03Map.cpp
#include
"T03Map.h"
/*
ValueMap是用来代替cocos2d.x的CCDictionary
*/
bool
T03Map::init()
{
Layer::init();
//内容的加载
ValueMap&
vm =
FileUtils::getInstance()->getValueMapFromFile("about.xml");
//CCDictionary* dict = CCDictionary::createWithContentsOfFile("about.xml");
//const CCString* x = dict->valueForKey("x");
//x->intValue();
//查找
auto
it =
vm.find("aaa");
if (it
== vm.end())
{
CCLog("can
not find aaa");
}
it =
vm.find("people3");
it->first;
//key:的类型是std::string
it->second;
//value:的类型是Value,相对cocos3.2.3的CCString
CCLog("key
is %s, value is %s",
it->first.c_str(),
it->second.asString().c_str());
CCLog("............................end");
vm["中文"]
= "bbb";
CCLog("........start
walk over");
//遍历
for (auto
it =
vm.begin();
it !=
vm.end();++it)
{
CCLog("key
is %s,value is %s",it->first.c_str(),it->second.asString().c_str());
}
CCLog("..........................end");
FileUtils::getInstance()->writeToFile(vm,
"new.xml");
#if 0
// C++11
for (auto
it :
vm)
{
it.first;
it.second;
}
//
插入
vm["aa"]
= 10;
//
访问,这种访问有副作用,如果bb节点不存在,它会创建一个bb节点
Value&
v =
vm["bb"];
v = 100;
vm["bb"]
= false;
#endif
return
true;
}
用到的about.xml如下:
people1
许佳音工作室出品
people2
总监:许佳音
people3
程序:姜博
people4
美术:马俊
people5
改编:班级
3
T04Label的用法
头文件:T04Label.h
#ifndef
__T04Label_H__
#define
__T04Label_H__
#include
"T32.h"
class
T04Label :public
Layer{
public:
CREATE_FUNC(T04Label);
bool
init();
};
#endif
编写:T04Label.cpp
#include
"T04Label.h"
bool
T04Label::init()
{
Layer::init();
{
Label*
label =
Label::createWithCharMap("fonts/Labelatlas.png",
24, 32, '0');
label->setString("12345");
addChild(label);
label->setPosition(winSize.width
/ 2, winSize.height
/ 2);
}
#if 0
Label*
label =
Label::createWithBMFont();
Label*
label =
Label::createWithSystemFont("aaa",
"Arial", 24);
Label*
label =
Label::createWithTTF("");
#endif
//Label* label = Label::createWithTexture()
return
true;
}
运行结果:
3
T05Touch触摸事件的用法