1. Cocos2dx3.2以后使用Vector<T>代替了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<Sprite*>
_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<Sprite*>::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如下:

<?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"

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触摸事件的用法

头文件:T05Touch.h

#ifndef
__T05Touch_H__

#define
__T05Touch_H__

#include
"T32.h"

class
T05Touch :public
Layer

{

public:

CREATE_FUNC(T05Touch);

bool
init();

void
TouchEnded(Touch*,Event
*);

};

#endif

编写:T05Touch.cpp

#include
"T05Touch.h"

bool
T05Touch::init()

{

Layer::init();

{

//
一般使用这种方式,和一个Node相关联

EventListenerTouchOneByOne*
ev =
EventListenerTouchOneByOne::create();

ev->onTouchBegan
= [](Touch*,
Event*){return
true; };

// 
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");
return
true; };

_eventDispatcher->addEventListenerWithFixedPriority(ev,
-128);

}

#endif

{

Sprite*
node =
Sprite::create();

addChild(node);

EventListenerTouchOneByOne*
ev =
EventListenerTouchOneByOne::create();

ev->onTouchBegan
= [](Touch*
touch,
Event*){

//通过touch->getLocation()的方式获得被选中的点的位置

Vec2
pt =
touch->getLocation();

CCLog("Sprite
is touched, pt.x=%f, pt.y=%f",
pt.x,
pt.y);

return
true;

};

// 
ev->onTouchEnded = [](Touch*, Event*){};

// ev->onTouchEnded = CC_CALLBACK_2(T05Touch::TouchEnded, this);

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev,
node);

}

{

EventListenerTouchAllAtOnce*
ev =
EventListenerTouchAllAtOnce::create();

ev->onTouchesBegan
= [](const
std::vector<Touch*>&,
Event*){};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev,
this);

}

return
true;

}

void
T05Touch::TouchEnded(Touch*,
Event*){

}

1.Cocos2dx 3.2中vector,ValueMap,Touch触摸时间的使用.iconv字符编解码的更多相关文章

  1. Cocos2d-x中Vector<T>容器以及实例介绍

    Vector<T> 是Cocos2d-x 3.x推出的列表容器,因此它所能容纳的是Ref及子类所创建的对象指针,其中的T是模板,表示能够放入到容器中的类型,在Cocos2d-x 3.x中T ...

  2. Cocos2d-x中Vector&lt;T&gt;容器以及实例介绍

    Vector<T> 是Cocos2d-x 3.x推出的列表容器,因此它所能容纳的是Ref及子类所创建的对象指针,其中的T是模板,表示能够放入到容器中的类型,在Cocos2d-x 3.x中T ...

  3. cocos2dx 3.2中的物理引擎初探(一)

    cocos2dx在设计之初就集成了两套物理引擎,它们是box2d和chipmunk.我目前使用的是最新版的cocos2dx 3.2.引擎中默认使用的是chipmunk,如果想要改使用box2d的话,需 ...

  4. c++中vector的用法详解

    c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...

  5. cocos2dx 3.7中 AppDelegate.h的class TestController;这种写法的具体意思不太明白,只能猜是类似于外部定义的东西。

    cocos2dx 3.7中 AppDelegate.h的class TestController;这种写法的具体意思不太明白,只能猜是类似于外部定义的东西.

  6. C++的STL中vector内存分配方法的简单探索

    STL中vector什么时候会自动分配内存,又是怎么分配的呢? 环境:Linux  CentOS 5.2 1.代码 #include <vector> #include <stdio ...

  7. 解决cocos2dx在Xcode中运行时报:convert: iCCP: known incorrect sRGB profile 的问题

    解决cocos2dx在Xcode中运行时报:convert: iCCP: known incorrect sRGB profile 的问题 本文的实践来源是参照了两个帖子完成的: http://dis ...

  8. cocos2d-x 3.0rc2中读取sqlite文件

    cocos2d-x 3.0rc2中读取sqlite文件的方式,在Android中直接读取软件内的会失败.须要复制到可写的路径下 sqlite3* dbFile = NULL; std::string ...

  9. C++ 中vector的基本用法

    //在网上看了好久,自己总结了一下下,第一篇博客,呼呼,学到不少 基本概念 vector容器是一个模板类,可以存放任何类型的对象).vector对象可以在运行时高效地添加元素,并且vector中元素是 ...

随机推荐

  1. redis安装异常的解决的办法

    在开始redis安装的时候,先废话一下 官网: 英文 :https://redis.io/ 中文 :http://www.redis.cn/ 首先我们需要一个linux服务器,当然windows也是可 ...

  2. SpringIOC学习一

    Spring是一个轻量级的控制反转(IOC)和面向切面(IOP)的容器框架1.控制反转IOC(inversion of controller)    IOC是一种概念,是把我们程序中类与类之间的依赖关 ...

  3. 机器学习技法:09 Decision Tree

    Roadmap Decision Tree Hypothesis Decision Tree Algorithm Decision Tree Heuristics in C&RT Decisi ...

  4. IO流大总结

    - - - - - - - - - - - - - - - 写在前面 - - - - - - - - - - - - - - - 1.概念 IO流用来处理设备之间的数据传输 Java对数据的操作是通过 ...

  5. Spring Boot简单应用——会员管理系统

    简介 本项目是使用Spring Boot编写的一个简单的会员管理系统. 提供了会员的解决方案,主要有会员模块,管理员模块,礼品模块,商品模块,会员等级模块,生日提醒模块,积分模块,详细模块如下图 准备 ...

  6. spring data学习

    在Spring Data模块中定义依赖: <dependencies> <dependency> <groupId>org.springframework.data ...

  7. Anaconda入门安装教程

    Anaconda 是什么? Anaconda 是一个可用于科学计算的 Python 发行版,支持 Linux.Mac.Windows系统,内置了常用的科学计算包.它解决了官方 Python 的两大痛点 ...

  8. HttpClient入门一

    HttpClient是一个实现了Http协议的功能强大的编程工具包. 要使用HttpClient,通常需要以下几部: 1.常见一个HttpClient实例 2.创建一个get或者post方法 3.告诉 ...

  9. [USACO12JAN]爬山Mountain Climbing

    题目描述 Farmer John has discovered that his cows produce higher quality milk when they are subject to s ...

  10. [洛谷]P3729 曼哈顿计划EX(最小割树/等价流树)

    题目大意:给出一张n个点m条边的无向图,每个点有点权,q次询问,每次给出k,要求选出若干个点点权之和不小于k,求一个最大的值x,使得选出的点中任意两点之间至少有x条互不相交的链.(n<=550, ...