cocos2dx3.2与2.3的区别和3.2中C++11新特性的应用

环境安装

如果电脑搭建好了cocos2d-x-2.2.3可以直接在上搭建3.2
本教程默认已安装好2.2.3
3,将上述两个文件解压到某目录下
4:命令行进入3.2目录#setup.py
根据提示操作

命令
任意目录命令#cocos显示cocos的帮助信息

#cocos new 查看帮助文档即可
#cocos run -s 源代码路径 -p 运行的平台 (win32, android, ios, mac linux)
#cocos run -s d:cocos\projects\TEST -p android   
     编译Android的,一般这个较有用,终于不用再借助eclipse啦。


3.2与2.2.3的区别

1:CCApplication -->Application  所有的CC前缀都去掉了。
2:Shard..--->getInstance
3:支持C11
4:Node一些接口改动比较大
     a:setZorder----->setLocalZorder
     b:旋转 setRotationX ----> setRotationSkewX
     c:boundingBox ----> getBoundingBox
5:枚举类型变了。比如:
enum class ResolutionPolicy
{
    // The entire application is visible in the specified area without trying to preserve the original aspect ratio.
    // Distortion can occur, and the application may appear stretched or compressed.
    EXACT_FIT,
    // The entire application fills the specified area, without distortion but possibly with some cropping,
    // while maintaining the original aspect ratio of the application.
    NO_BORDER,
    // The entire application is visible in the specified area without distortion while maintaining the original
    // aspect ratio of the application. Borders can appear on two sides of the application.
    SHOW_ALL,

变成了枚举类类型,所以用的时候要加上类作用域:
glview->setDesignResolutionSize(480, 320, ResolutionPolicy::EXACT_FIT);

6:没有ccp了,setPostion(ccp(0, 0)); ----> setPostion(0, 0);

7:触摸改动比较大:
	// 触摸
	auto ev = EventListenerTouchOneByOne::create();
#if 0
	ev->onTouchBegan = [](Touch*, Event*){
		return true;
	};
#endif

	//ev->onTouchBegan = std::bind(&TMenu::TouchBegan, this, std::placeholders::_1, std::placeholders::_2);

	ev->onTouchBegan = CC_CALLBACK_2(TMenu::TouchBegan, this);

	ev->onTouchMoved = [&](Touch* touch, Event*){
		setPositionY(getPositionY() + touch->getDelta().y);
	};
	//加入到事件分发器
	_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);


8:CClog --->cocos2d::log
{
		// 一般使用这种方式,和一个Node相关联
		EventListenerTouchOneByOne* ev = EventListenerTouchOneByOne::create();
		ev->onTouchBegan = [](Touch*, Event*){return true; };
		//	ev->onTouchEnded = [](Touch*, Event*){};
		ev->onTouchEnded = CC_CALLBACK_2(T05Touch::TouchEnded, this);
		//跟Zorder相关,比如在Layer上,随着Layer的消失而消失
		_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);
	}
#if 0
	{
		// 固有优先级的方式使用比较少
		EventListenerTouchOneByOne* ev = EventListenerTouchOneByOne::create();
		//设置吞噬
		ev->setSwallowTouches(true);

		ev->onTouchBegan = [](Touch*, Event*){CCLog("Touch Begin"); return true; };
		//固有的优先级,比如在Layer上,就算Layer消失了,这个触摸都还在。所以不怎么常用。
		_eventDispatcher->addEventListenerWithFixedPriority(ev, -128);
	}
#endif
	{
		//注意,在3.2里面触摸的对象可以是任意了,不在局限于Layer。比如此处我们设置触摸的对象是Sprite,
		//会发现一个问题,当我没点击在精灵上,而点击在其他地方时,也会触发,cocos就是这样设计的。
		Sprite* node = Sprite::create();
		addChild(node);

		EventListenerTouchOneByOne* ev = EventListenerTouchOneByOne::create();
		ev->onTouchBegan = [](Touch* touch, Event*){

			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);
	}



9:Point与Vec2是一样的。


C++11新特性的应用

1: Lambada:

	// Lambada
		auto func = []{return 1; };
		int i = func(); //[]{return 1; }();
	}
	// 最简单的lambada表达式是只要一个中括号和一个大括号
	// [] 捕获列表
	// {} 函数体
	// 1.捕获列表,可以放变量名
	{
		int v = 100;
		auto func = [v]{return v; };
		int x = func();
	}
	// 2.捕获列表,可以捕获多个变量
	{
		int p = 100, q = 200;
		auto func = [p, q]{return p + q; };
		int s = func();
	}
	// 3.捕获列表,有引用和传值两种方式,传值不可以改变,引用可以改变,并且改变外部的变量值
	{
		int p = 100, q = 200;
		auto func = [p, &q]{q++;  return p + q; };
		int s = func();
	}
	// 4.捕获列表,可以定义mutable(易变的,不定的)类型的lambada,能改变传值的捕获参数,但是不能改变外部的变量值
	{
		int p = 100, q = 200;
		auto func = [p, q]()mutable{ p++; q++; return p + q; };
		int s = func();
		CCLog("p=%d, q=%d, s=%d", p, q, s);
	}
	// 5.捕获列表, 可以用=或者&捕获所有变量,=指传值,&表示引用
	{
		int p = 100, q = 200;
		auto func = [&]{
			return p + q; //用了&后可以捕获外部的所有变量,包括this指针,所以这点非常常用哦!
		};
	}

	// 稍微复杂点的lambada表达式
	{
		auto add = [](int v1, int v2){return v1 + v2; };
		auto a = add(1, 2);
	}

	// 小括号中的是参数列表,参数列表和捕获列表区别在于,参数列表的参数由调用方决定,捕获列表由定义方决定。
	// 所以更加灵活

	// 更加复杂的lambada表达式,有返回值,返回值一般都省略。 ->int 表示返回int
	{
		auto add = [](int v1, int v2)->int{ return v1 + v2; };
	}

	// 总结:auto func = [](){};
	{
		auto func = [](){};
	}



2:  std::function    std::bind

是一个模板类
C++里不建议用函数指针了
 std::function:
	void foo(){
		cocos2d::log("foo is called\n");
	}
	// 函数指针类型
	std::function<void()> func = foo;

std::bind
	// bind的功能,就是把一个具体函数,变成std::function对象
	// bind可以把具体函数和std::function形式完全改变,比如参数数量的改变
	void funArg3(int n, char c, float f)
	{
		CCLog("%d, %c, %f", n, c, f);
	}
	{
		std::function<void()> func = std::bind(funArg3, 100, 'c', 0.1f);
		func();
	}

	// 也可以改变参数顺序
	{
		std::function<void(float, char, int)> func = std::bind(funArg3, 
			std::placeholders::_3, std::placeholders::_2, std::placeholders::_1);//std::placeholders::_1占位符,指的调用者的第一个参数,比如此处的“float”或者“1.0f”。
		func(1.0f, 'd', 2000);
	}
	// 也可以同时改变参数个数和顺序
	{
		std::function<void(float, char)> func = std::bind(funArg3,
			100, std::placeholders::_2, std::placeholders::_1);
		func(4.0f, 'x');
	}

	//改变参数这些有什么用呢?为了把调用方跟被调方隔离,不管你那边什么样,我bind一下按我这边的调用,高内聚低耦合。

	// 也可以绑定成员函数
	{
		std::function<void()> func = std::bind(&T01CPP11::mFoo, this);
		func();
	}
	return true;
}

void T01CPP11::mFoo()
{
	CCLog("mFoo is called");
}

终极目标:
	// 触摸
	auto ev = EventListenerTouchOneByOne::create();
#if 0
	ev->onTouchBegan = [](Touch*, Event*){
		return true;
	};
#endif

	//ev->onTouchBegan = std::bind(&TMenu::TouchBegan, this, std::placeholders::_1, std::placeholders::_2);

	//每次上面那样写比较烦,cocos提供了宏CC_CALLBACK_2     _2的宏是指有两个参数,类推
	//绕晕的话,这就是终极目标
	ev->onTouchBegan = CC_CALLBACK_2(TMenu::TouchBegan, this);

	ev->onTouchMoved = [&](Touch* touch, Event*){
		setPositionY(getPositionY() + touch->getDelta().y);
	};
	//加入到事件分发器
	_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

	return true;
}

bool TMenu::TouchBegan(/*TMEnu* this, */Touch*, Event*)
{
	return true;
}


3:Vector

在3.2版本中CCARRAY已经没有了,Vector代替
	Vector<Sprite*> _arr;
	
	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);

4:ValueMap

CCditionary没有了,被ValueMap代替
// 内容的加载
	ValueMap& vm = FileUtils::getInstance()->getValueMapFromFile("about.xml");
	//CCDictionary* dict = CCDictionary::createWithContentsOfFile("about.xml");
	// const CCString*...
	//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,相对cocos2.2.3的CCString
	CCLog("key is %s, value is %s", it->first.c_str(), it->second.asString().c_str());

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

	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()->setFilenameLookupDictionary(vm);
	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


5:Label

将以前的那些各种label集中到一个了:Label
	{
		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()









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值