1.Cocos2dx 3.2中vector,ValueMap,Touch触摸时间的使用.iconv字符编解码
来源:互联网 发布:梅西大学怎么样 知乎 编辑:程序博客网 时间:2024/05/01 09:28
Cocos2dx3.2以后使用Vector<T>代替了CCArray。案例如下:
头文件:T02Vector.h
#ifndef__T02Vector_H__
#define__T02Vector_H__
#include"T32.h"
classT02Vector : publicLayer
{
public:
CREATE_FUNC(T02Vector);
//Cocos2dx3.2以后使用Vector代替了CCArray
Vector<Sprite*>_arr;
boolinit();
};
#endif
编写:T02Vector.cpp
#include"T02Vector.h"
//in cocos3.2 Vector代替CCArray
//如果不是Ref的子类,那不能用Vector,应该用std::vector
boolT02Vector::init()
{
Layer::init();
Sprite*sprite = Sprite::create();
//增加元素
_arr.pushBack(sprite);
//遍历
Vector<Sprite*>::iteratorit;
for (it = _arr.begin();it != _arr.end(); ++it)
{
Sprite*s = *it;
}
for (autoit = _arr.begin();it != _arr.end();++it)
{
}
for (autoit:_arr)
{
}
//从后往前遍历
for (autoit = _arr.rbegin();it != _arr.rend();++it)
{
}
//删除
_arr.eraseObject(sprite);
returntrue;
}
2 Map的用法(注意字符编解码的第三方库有:iconv,cocos中集成的有这方面的功能)
头文件:T03Map.h
#ifndef__T03Map_H__
#define__T03Map_H__
#include"T32.h"
classT03Map :publicLayer{
public:
CREATE_FUNC(T03Map);
boolinit();
};
#endif
编写:T03Map.cpp
#include"T03Map.h"
/*
ValueMap是用来代替cocos2d.x的CCDictionary
*/
boolT03Map::init()
{
Layer::init();
//内容的加载
ValueMap&vm =FileUtils::getInstance()->getValueMapFromFile("about.xml");
//CCDictionary* dict = CCDictionary::createWithContentsOfFile("about.xml");
//const CCString* x = dict->valueForKey("x");
//x->intValue();
//查找
autoit =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 (autoit =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 (autoit :vm)
{
it.first;
it.second;
}
//插入
vm["aa"] = 10;
//访问,这种访问有副作用,如果bb节点不存在,它会创建一个bb节点
Value&v =vm["bb"];
v = 100;
vm["bb"] = false;
#endif
returntrue;
}
用到的about.xml如下:
<?xml version="1.0" encoding="UTF-8" ?>
<plist>
<dict>
<key>people1</key>
<string>许佳音工作室出品</string>
<key>people2</key>
<string>总监:许佳音</string>
<key>people3</key>
<string>程序:姜博</string>
<key>people4</key>
<string>美术:马俊</string>
<key>people5</key>
<string>改编:班级</string>
</dict>
</plist>
3 T04Label的用法
头文件:T04Label.h
#ifndef__T04Label_H__
#define__T04Label_H__
#include"T32.h"
classT04Label :publicLayer{
public:
CREATE_FUNC(T04Label);
boolinit();
};
#endif
编写:T04Label.cpp
#include"T04Label.h"
boolT04Label::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()
returntrue;
}
运行结果:
3 T05Touch触摸事件的用法
头文件:T05Touch.h
#ifndef__T05Touch_H__
#define__T05Touch_H__
#include"T32.h"
classT05Touch :publicLayer
{
public:
CREATE_FUNC(T05Touch);
boolinit();
voidTouchEnded(Touch*,Event *);
};
#endif
编写:T05Touch.cpp
#include"T05Touch.h"
boolT05Touch::init()
{
Layer::init();
{
//一般使用这种方式,和一个Node相关联
EventListenerTouchOneByOne*ev =EventListenerTouchOneByOne::create();
ev->onTouchBegan = [](Touch*,Event*){returntrue; };
// ev->onTouchEnded = [](Touch*, Event*){};
ev->onTouchEnded = CC_CALLBACK_2(T05Touch::TouchEnded,this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(ev,this);
}
#if 0
{
//固有优先级的方式使用比较少
EventListenerTouchOneByOne*ev =EventListenerTouchOneByOne::create();
ev->setSwallowTouches(true);
ev->onTouchBegan = [](Touch*,Event*){CCLog("Touch Begin");returntrue; };
_eventDispatcher->addEventListenerWithFixedPriority(ev, -128);
}
#endif
{
Sprite*node =Sprite::create();
addChild(node);
EventListenerTouchOneByOne*ev =EventListenerTouchOneByOne::create();
ev->onTouchBegan = [](Touch*touch,Event*){
//通过touch->getLocation()的方式获得被选中的点的位置
Vec2pt =touch->getLocation();
CCLog("Sprite is touched, pt.x=%f, pt.y=%f",pt.x,pt.y);
returntrue;
};
// ev->onTouchEnded = [](Touch*, Event*){};
// ev->onTouchEnded = CC_CALLBACK_2(T05Touch::TouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(ev,node);
}
{
EventListenerTouchAllAtOnce*ev =EventListenerTouchAllAtOnce::create();
ev->onTouchesBegan = [](conststd::vector<Touch*>&,Event*){};
_eventDispatcher->addEventListenerWithSceneGraphPriority(ev,this);
}
returntrue;
}
voidT05Touch::TouchEnded(Touch*,Event*){
}
- 1.Cocos2dx 3.2中vector,ValueMap,Touch触摸时间的使用.iconv字符编解码
- Cocos2dx中ValueMap的使用
- 浅谈容器Vector ,话说ValueMap的一般使用。Cocos2dx 3.0 过渡篇
- Cocos2dx 3.0 话说ValueMap的一般使用
- Cocos2dx 学习随笔(一) Cocos2dx 3.x 读取plist文件 ValueVector 与 ValueMap的使用
- 说说Qt中中文字符的编解码那些事
- cocos2dx中使用触摸事件
- [cocos2dx]在cocos2dx中使用iconv库解决中文乱码
- 话说ValueMap的一般使用
- Spring中html字符编解码
- Cocos2dx 3.0 过渡篇(三十二)话说ValueMap的一般使用
- opus编解码的使用
- 【转】字符编解码的故事
- cocos2d-x中文显示问题解决方法(使用cocos2dx中自带的iconv实现)
- cocos2d-x中文显示问题解决方法(使用cocos2dx中自带的iconv实现)
- 字符编解码故事
- 关于字符编解码
- cocos2dx中使用iconv转码(win32,iOS,Android)
- Mac Yosemite 10.10下无法安装MySQL
- Android按键事件处理流程 -- KeyEvent
- Android TouchEvent事件传递机制
- C语言的关键字typedef的用法
- ZOJ 3329 One Person Game 概率dp 处理环
- 1.Cocos2dx 3.2中vector,ValueMap,Touch触摸时间的使用.iconv字符编解码
- IOS学习笔记2—Objective C—类、属性、方法
- 黑马程序员——面向对象<一> 笔记第三篇
- Android touch mode和focusableInTouchMode分析
- devstack方式部署all-in-one openstack如何修改instances的目录
- sgu132:Another Chocolate Maniac
- CocoaPods详解之----进阶篇
- 端口被占用的处理方法
- hibernate与Mysql-Dialect问题