Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了总结Cocos2d-x 3.x版本的一些变化大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

【Cocos2d-x v3.0 亮点】

  • 使用 C++(C++11) 的特性取代了 Objective-C 的特性

  • 优化了 Labels

  • 优化了渲染器(比 v2.2 更快)

  • 新的事件分发机制

  • 物理引擎集成

  • 新的 UI 对象

  • 模板容器

*使用 cocos2d::Map<> 替代了 CCDictionary,用法

*使用 cocos2d::Vector<> 替代了 CCArray,serif; font-size:14px"> *使用 cocos2d::Value 替代了 CCBool,CCFLoat,CCDouble,serif; font-size:14px">

1、C++11 特性

C++11 FAQ请查看这里:http://www.stroustrup.com/C++11FAQ.html

1.1、新的关键字及语法

(1)nullptr

nullptr是为了解决NULL的二义性,因为NULL实际上代表的是0。

1
2
@H_616_75@ 3
4
5
6
void f( int ); //#1
char *); //#2
//C++03
f(0); //二义性
//C++11
f(nullptr) //无二义性,调用f(char*)

(2)auto

根据上下文自动类型推导。

5
//v2.x
CCSprite*pSprite=CCSprite::create( "HelloWorld.png" );
//v3.x
autopSprite=Sprite::create( );

(3)decltype

decltype与此相反,从变量或表达式中获取类型。

2
x=3;
decltype(X)y=x;

(4)override

派生类重写基类的虚函数时,在函数的声明中加上override(非必须)。

这样可在编译时检测出对基类函数的错误重写。

6
7
8
9
10
struct B{
virtual f();
g() const ;
k(); //notvirtual
};
D:B{
f()override; //OK:overridesB::f()
g()override; //error:wrongtype
k()override; //error:B::k()isnotvirtual
};

(5)final

可用来修饰基类的虚函数,表示该函数不可被派生类重写即override。

9
f() final; //donotoverride
g();
};
; //error:D::fattemptstooverridefinalB::f
g(); //OK
(6)序列for循环

在C++中for循环可以使用类似java的简化的for循环。

可以用于遍历数组,容器,String以及由begin和end函数定义的序列(即有Iterator)。

示例代码如下

4
@H_678_89@map<String, >m{{ "a" ,1},{ "b" "c" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,3}};
for (autop:m){
cout<<p.first<< ":" <<p.second<<endl;
}

(7)lambda表达式

类似Javascript中的闭包,它可以用于创建并定义匿名的函数对象,以简化编程工作。

Lambda语法:[函数对象参数](操作符重载函数参数)->返回值类型{ 函数体 }

例:[](int a,int b){ return a > b; }

运用在MenuItem的回调函数:

autocloseItem=MenuItemImage::create("CloseNormal.png":1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"CloseSELEcted.png" :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
[](Object*sender)
{
Director::geTinstance()->end(); //直接在这里添加按钮要调用的代码
});

1.2 标准库 std::function 与 std::bind

  • std::function :可以定义类似函数指针的类型

  • std::bind:可以方便的绑定类的成员函数

这个常在Cocos2d-x中的回调函数中使用:

  • CallFunc 可以由 std::function<void()> 来创建。

  • CallFuncN 可以由 std::function<void(Node*)> 来创建。

  • CallFuncND和CallFuncO已经被移除,它们可以类似地由CallFuncN和CallFunc来创建。

  • MenuItem 支持 std::function<void(Node*)> 作为回调。

std::function<(std::vector<Touch*>&,Event*)>onTouchesBegan;
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,Event*)>onTouchesMoved;
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,Event*)>onTouchesEnded;
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,Event*)>onTouchesCancelled;
//newcallBACksbasedonC++11
#defineCC_CALLBACK_0(__SELEctor__,__target__,...)std::bind(&__SELEctor__,##__VA_ARGS__)
#defineCC_CALLBACK_1(__SELEctor__,std::placeholders::_1,##__VA_ARGS__)
#defineCC_CALLBACK_2(__SELEctor__,std::placeholders::_2,##__VA_ARGS__)
#defineCC_CALLBACK_3(__SELEctor__,std::placeholders::_3,##__VA_ARGS__)

MenuItem 示例:

10
11
12
13
14
//v2.1版本
CCMenuItemLabel*item=CCMenuItemLabel::create(label, this :1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,menu_SELEctor(MyClass::callBACk));
//v3.0版本(短版本)
autoitem=MenuItemLabel::create(label,CC_CALLBACK_1(MyClass::callBACk,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important">));
//v3.0版本(长版本)
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,std::bind(&MyClass::callBACk,std::placeholders::_1));
//v3.0中你也可以使用lambda表达式或者其他函数对象
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
[&](Object*sender){
//dosomething.Item"sender"clicked
1.3 强类型枚举

以 k 开头的常量和枚举量,通常被定义为 int 或者简单的 enum 类型,现在已经被强类型枚举(enum class)所替代,这样有利于避免冲突和类型错误。

新的格式是:

@H_541_618@

新的格式是:

示例:


2、去OC化

2.1 移除"CC"前缀以及free functions

(1)移除C++类的"CC"前缀

(2)free functions的变更

  • 对于 drawing primitives:

*已经被添加到 DrawPrimitives 命名空间

*移除 cc 前缀

  • 对于 gl proxy functions:

*已经被添加到 GL 命名空间

*移除 ccGL 前缀

示例:

2.2 使用 clone 替代 copy

  • clone():返回了一份 autoreleased 版本的拷贝。

  • copy():不再被支持,如果你使用它,仍然是可以编译的,但是代码会崩掉。

2.3 单例类采用了 geTinstance 和 destroyInstance

  • geTinstance:替代 shared***

  • destroyInstance:替代 end***

2.4 使用 Ref 代替了 Object

因为 Object 容易让人混淆,所以重命名为 Ref ,同时移除了和引用计数无关的函数,之前所有继承于 Object 的类现在都改为继承于 Ref。

2.5 getters

Getters 现在使用了 get 前缀。

当然 getters 在声明中也被标识为 const 。

示例:

1
2
@H_616_75@ 3
4
//v2.1
virtual float getScale();
//v3.0
getScale() const ;

2.6 POD 类型

接收 POD 类型作为参数的方法(比如:TexParams,Point,Size,等等)已经修改为传递成 const 型引用。

voidsetTexParameters(ccTexParams*texParams);
setTexParameters( ccTexParams&texParams);

3、新的渲染器

3.1 自动批处理

自动批处理功能意味着 渲染器将会把 多次绘制调用 打包为一次 大的绘制调用(AKA batch)。

组合绘制调用当然需要满足一定的条件:

  • 它仅工作在QuadCommand命令下(由Sprite和ParticleSystem对象使用);

  • QuadCommadnds 必须共享相同的材质ID:相同纹理ID、相同GLProgram、相同混合功能;

  • QuadCommands 必须是连续的;

如果这些条件都满足的话,渲染器 将会使用所有这些 QuadCommand 对象创建一个批处理(一次绘制调用)。

如果你不熟悉 OpenGL 的使用,批处理对于您游戏的能否拥有一个流畅的运行速度是很重要的,越少的批处理(绘制调用)越有利于您游戏的表现力。

3.2 自动剔除

目前,自动剔除功能只在 Sprite 对象中实现。

当 Sprite::draw() 被调用的时候,它将会检查 Sprite 是否超出屏幕,如果是的话,它将不会发送 QuadCommand 命令给 渲染器,因此可以获得一些性能上的提升。

3.3 全局 Z 值

  • Node 增加了新的函数 setGlobalZOrder() / getGlobalZOrder() 。

  • setZOrder() / getZOrder() 被替代为 setLocalZOrder() / getLocalZOrder() 。

  • globalZOrder 是一个float(不是int)的参数。这个值在渲染器中用来给RenderCommand排序。较低的值拥有较高的优先级。这意味着一个globalZorder为-10的节点会比一个globalZOrder为10 的节点优先绘制。

  • globalZOrder 为 0 (默认值)的节点将会根据 Scene Graph 顺序绘制。

  • 如果 globalZOrder 不变的话,Cocos2d-x v3.0 和 Cocos2d-x v2.2 行为一致。

  • globalZOrder() 和 localZOrder():

* globalZOrder是用于渲染器中用来给“绘制命令”排序的

* localZOrder是用于父节点的子节点数组中给节点对象排序的

3.4 Sprite 和 SpriteBatchNode

v2.2版本中推荐的优化游戏方式是将 SpriteBatchNode 对象设置为 Sprite 对象的父节点。

然使用 SpriteBatchNode 对象仍然是一个非常好的优化游戏的方式。

但是它仍然有一定的限制:

  • Sprite对象的孩子只能是Sprite(否则,Cocos2d-x会触发断言)

* Sprite父节点是SpriteBACtchNode时,ParticleSystem不能作为Sprite的子节点。

* 这将导致当Sprite父节点是SpriteBatchNode时,不能使用ParallaxNode

  • 所有的 Sprite 对象必须共享相同的纹理ID (否则,Cocos2d-x 会触发断言)

  • Sprite 对象使用 SpriteBatchNode 的混合函数和着色器。

然v3.0仍然支持SpriteBatchNode(与之前版本拥有相同的特效和限制),但我们不鼓励使用它。相反,我们推荐直接使用Sprite,不需要将它作为子节点添加到SpriteBatchNode中。

但是,为了能让v3.0有更好的表现,你必须要确保你的Sprite对象满足以下条件:

  • 贡献相同的纹理ID(把它们放在一个spritesheet中,就像使用 SpriteBatchNode 一样)

  • 确保它们使用相同的着色器和混合函数(就像使用 SpriteBatchNode 一样)

如果这么做,Sprite将会像使用SpriteBatchNode一样的快。(在旧设备上大概慢了10%,在新设备上基本上察觉不出)

v2.2 和 v3.0 最大的区别在于

  • Sprite 对象可以有不同的纹理ID。

  • Sprite 对象可以有不同种类的 Node 作为子节点,包括 ParticleSystem。

  • Sprite 对象可以有不同的混合函数和不同的着色器。

但是如果你这么做,渲染器 可能无法对它所有的子节点进行批处理(性能较低)。但是游戏仍然可以正常运行,不会触发任何断言。

总结:

  • 保持将所有的精灵放在一张大的 spritesheet 中。

  • 使用相同的混合函数(使用默认)

  • 使用相同的着色器(使用默认)

  • 不要将精灵添加到 SpriteBatchNode

    @H_541_618@ @H_673_965@4、优化 LabelTTF / LabelBMFont / LabelAtlas

    LabelTTF,LabelBMFont 和 LabelAtlas 将会被新的Label代替。

    新的Label 带来的好处有:

    • 统一了创建 LabelTTF,LabelBMFont 和 LabelAtlas 的 API 。

    • 使用freetype生成labels的纹理,保证了在不同平台下labels有相同的效果。

    • 缓存纹理以提高性能


    @H_673_965@5、新的事件分发机制

    触摸事件,键盘事件,加速器事件和自定义事件等所有事件都由EventDispatcher分发。

    TouchDispatcher,KeypadDispatcher,KeyboardDispatcher,AccelerometerDispatcher已被移除。

    EventDispatcher 的特性主要有:

    • 事件的分发基于渲染顺序

    • 所有的事件都由 EventDispatcher 分发

    • 可以使用 EventDispatcher 来分发自定义事件

    • 可以注册一个 lambda 表达式作为回调函数

大佬总结

以上是大佬教程为你收集整理的总结Cocos2d-x 3.x版本的一些变化全部内容,希望文章能够帮你解决总结Cocos2d-x 3.x版本的一些变化所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。