【Cocos2d-x v3.0 亮点】
*使用 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
3
4
5
6
|
void
f(
int
);
char
*);
f(0);
f(nullptr)
|
(2)auto
根据上下文自动类型推导。
5
CCSprite*pSprite=CCSprite::create(
"HelloWorld.png"
);
autopSprite=Sprite::create(
);
|
(3)decltype
decltype与此相反,从变量或表达式中获取类型。
(4)override
派生类重写基类的虚函数时,在函数的声明中加上override(非必须)。
这样可在编译时检测出对基类函数的错误重写。
6
7
8
9
10
struct
B{
virtual
f();
g()
const
;
k();
};
D:B{
f()override;
g()override;
k()override;
};
|
(5)final
可用来修饰基类的虚函数,表示该函数不可被派生类重写即override。
9
f()
final;
g();
};
;
g();
(6)序列for循环
在C++中for循环可以使用类似java的简化的for循环。
可以用于遍历数组,容器,string以及由begin和end函数定义的序列(即有Iterator)。
示例代码如下:
4
map<string,
>m{{
"a"
,1},{
"b"
"c"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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}};
cout<<p.first<<
":"
<<p.second<<endl;
}
|
(7)lambda表达式
类似Javascript中的闭包,它可以用于创建并定义匿名的函数对象,以简化编程工作。
Lambda语法:[函数对象参数](操作符重载函数参数)->返回值类型{ 函数体 }
举例:[](int a,int b){ return a > b; }
运用在MenuItem的回调函数:
autocloseItem=MenuItemImage::create(
"Closenormal.png"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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
这个常在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;
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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;
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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;
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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;
#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
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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">));
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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));
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height: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){
1.3 强类型枚举
以 k 开头的常量和枚举量,通常被定义为 int 或者简单的 enum 类型,现在已经被强类型枚举(enum class)所替代,这样有利于避免冲突和类型错误。
新的格式是:
新的格式是:
示例:
2、去OC化
2.1 移除"CC"前缀以及free functions
(1)移除C++类的"CC"前缀
(2)free functions的变更
*已经被添加到 DrawPrimitives 命名空间
*移除 cc 前缀
*已经被添加到 GL 命名空间
*移除 ccGL 前缀
示例:
2.2 使用 clone 替代 copy
2.3 单例类采用了 getInstance 和 destroyInstance
2.4 使用 Ref 代替了 Object
因为 Object 容易让人混淆,所以重命名为 Ref ,同时移除了和引用计数无关的函数,之前所有继承于 Object 的类现在都改为继承于 Ref。
2.5 getters
Getters 现在使用了 get 前缀。
当然 getters 在声明中也被标识为 const 。
示例:
1
2
3
4
|
virtual
float
getScale();
getScale()
const
;
|
2.6 POD 类型
接收 POD 类型作为参数的方法(比如:TexParams,Point,Size,等等)已经修改为传递成 const 型引用。
void
setTexParameters(ccTexParams*texParams);
3、新的渲染器
3.1 自动批处理
自动批处理功能意味着 渲染器将会把 多次绘制调用 打包为一次 大的绘制调用(AKA batch)。
组合绘制调用当然需要满足一定的条件:
如果这些条件都满足的话,渲染器 将会使用所有这些 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父节点是SpriteBactchNode时,ParticleSystem不能作为Sprite的子节点。
* 这将导致当Sprite父节点是SpriteBatchNode时,不能使用ParallaxNode
虽然v3.0仍然支持SpriteBatchNode(与之前版本拥有相同的特效和限制),但我们不鼓励使用它。相反,我们推荐直接使用Sprite,不需要将它作为子节点添加到SpriteBatchNode中。
但是,为了能让v3.0有更好的表现,你必须要确保你的Sprite对象满足以下条件:
如果这么做,Sprite将会像使用SpriteBatchNode一样的快。(在旧设备上大概慢了10%,在新设备上基本上察觉不出)
v2.2 和 v3.0 最大的区别在于:
但是如果你这么做,渲染器 可能无法对它所有的子节点进行批处理(性能较低)。但是游戏仍然可以正常运行,不会触发任何断言。
总结:
相关文章
本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1. 来源 QuickV3sample项目中的2048样例游戏,以及最近《...
Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...
| |