1.Cocos2dx 3.2中vector,ValueMap,Touch触摸时间的使用.iconv字符编解码

前端之家收集整理的这篇文章主要介绍了1.Cocos2dx 3.2中vector,ValueMap,Touch触摸时间的使用.iconv字符编解码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

  1. @H_403_7@Cocos2dx3.2以后使用@H_403_7@Vector<T>代替了@H_403_7@CCArray。案例如下:

头文件:T02Vector.h

#ifndef __T02Vector_H__

#define __T02Vector_H__

#include "T32.h"

class T02Vector : public Layer

{

public:

CREATE_FUNC(T02Vector);

//Cocos2dx3.2以后使用@H_403_7@Vector代替了@H_403_7@CCArray

Vector<Sprite*> _arr;

bool init();

};

#endif

编写:@H_403_7@T02Vector.cpp

#include "T02Vector.h"

//in cocos3.2 Vector代替@H_403_7@CCArray

//如果不是@H_403_7@Ref的子类,那不能用@H_403_7@Vector,应该用@H_403_7@std::vector

bool T02Vector::init()

{

Layer::init();

Sprite* sprite = Sprite::create();

//增加元素

_arr.pushBack(sprite);

//遍历

Sprite*>::iterator it;

for (it = _arr.begin(); it != _arr.end(); ++it)

{

Sprite* s = *it;

}

for (auto it = _arr.end();++it)

{

}

for (auto it: _arr)

{

}

//从后往前遍历

for (auto it = _arr.rbegin(); it != _arr.rend();++it)

{

}

//删除

_arr.eraSEObject(sprite);

return true;

}

@H_403_7@2 Map用法(注意字符编解码的第三方库有:@H_403_7@iconv@H_403_7@cocos中集成的有这方面的功能

文件@H_403_7@T03Map.h

#ifndef __T03Map_H__

#define __T03Map_H__

#include "T32.h"

class T03Map : public Layer{

public:

CREATE_FUNC(T03Map);

bool init();

};

#endif

编写:@H_403_7@T03Map.cpp

#include "T03Map.h"

/*

ValueMap是用来代替@H_403_7@cocos2d.x的@H_403_7@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."people3");

it->first; //key:的类型是@H_403_7@std::string

it->second; //value:的类型是@H_403_7@Value,相对@H_403_7@cocos3.2.3的@H_403_7@CCString

"key is %s,value is %s",it->first.c_str(),it->second.asString().c_str());

"............................end");

vm["中文@H_403_7@"] = "bbb";

"........start walk over");

//遍历

for (auto it = vm.begin(); it != vm.end();++it)

{

c_str());

}

"..........................end");

writeToFile(vm,"new.xml");

#if 0

// C++11

for (auto it : vm)

{

it.first;

it.second;

}

// 插入

vm["aa"] = 10;

// 访问,这种访问有副作用,如果@H_403_7@bb节点不存在,它会创建一个@H_403_7@bb节点

Value& v = vm["bb"];

v = 100;

vm["bb"] = false;

#endif

return true;

}

用到的@H_403_7@about.xml如下:

@H_403_7@<?xml version="1.0" encoding="UTF-8" ?>

@H_403_7@<plist>

@H_403_7@<dict>

@H_403_7@<key>people1</key>

@H_403_7@<string>许佳音工作室出品@H_403_7@</string>

@H_403_7@<key>people2</key>

@H_403_7@<string>总监:许佳音@H_403_7@</string>

@H_403_7@<key>people3</key>

@H_403_7@<string>程序:姜博@H_403_7@</string>

@H_403_7@<key>people4</key>

@H_403_7@<string>美术:马俊@H_403_7@</string>

@H_403_7@<key>people5</key>

@H_403_7@<string>改编:班级@H_403_7@</string>

@H_403_7@</dict>

@H_403_7@</plist>

@H_403_7@3 T04Label用法

文件@H_403_7@T04Label.h

__T04Label_H__

#define __T04Label_H__

#include T04Label :public T04Label);

bool

编写:@H_403_7@T04Label.cpp

"T04Label.h"

bool T04Label::init();

{

Label* label = Label::createWithCharMap("fonts/Labelatlas.png",24,32,21); font-family:新宋体; font-size:9.5pt">'0');

label->setString("12345");

addChild(label);

label->setPosition(winSize.width / 2,winSize.height / 2);

}

#if 0

createWithBMFont();

createWithSystemFont("aaa",21); font-family:新宋体; font-size:9.5pt">"Arial",24);

createWithTTF("");

#endif

//Label* label = Label::createWithTexture()

return true;

}

运行结果:

@H_403_7@3 T05Touch触摸事件的用法

文件@H_403_7@T05Touch.h

__T05Touch_H__

#define __T05Touch_H__

#include T05Touch :public Layer

{

public:

T05Touch);

bool init();

void TouchEnded(Touch*,Event *);

};

#endif

编写:@H_403_7@T05Touch.cpp

"T05Touch.h"

bool T05Touch::init();

{

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

EventListenerTouchOneByOne* ev = EventListenerTouchOneByOne::create();

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

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

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

_eventDispatcher->addEventListenerWithSceneGraPHPriority(ev,this);

}

#if 0

{

// 固有优先级的方式使用比较少

create();

ev->setSwallowTouches(true);

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

_eventDispatcher->addEventListenerWithFixedPriority(ev,-128);

}

#endif

{

Sprite* node = Sprite::create();

addChild(node);

Touch* touch,133); font-family:新宋体; font-size:9.5pt">Event*){

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

Vec2 pt = touch->getLocation();

"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->node);

}

{

EventListenerTouchAllAtOnce* ev = EventListenerTouchAllAtOnce::create();

ev->onTouchesBegan = [](const std::vector<Touch*>&,133); font-family:新宋体; font-size:9.5pt">Event*){};

_eventDispatcher->this);

}

return true;

}

void Event*){

}

猜你在找的Cocos2d-x相关文章