cocos2d-x cpp 3.*相较于2.*的区别(整理)

截至笔者写这篇文章的时间,cocos2d-x 的两个最新版本 分别是

Download v3.0rc1 

Download v2.2.3

运行环境需求:

  • Android 2.3 or newer
  • iOS 5.0 or newer
  • OS X 10.7 or newer
  • Windows (which version?)
  • Linux Ubuntu 12.04 (or newer)
编译环境需求:
  • Xcode 4.6 (for iOS or Mac)
  • gcc 4.7 for Linux or Android. For Android ndk-r8e or newer is required.
  • Visual Studio 2012 (for Windows)

重要更新内容:
  • Replace Objective-C patters with C++ (C++11) patterns and best practices
  • Improve Labels
  • Improve renderer
其中C++ 11 新特性:

A subset of C++11 features are being used in cocos2d-x:

  • std::function, including lambda objects for callbacks
  • strongly typed enums, for most of the cocos2d-x enums and constants
  • std::thread for threading
  • override context keyword, for overriden methods

st  std::function

  • CallFunc can be created with an std::function<void()>
  • CallFuncN can be created with an std::function<void(Node*)>
  • CallFuncND and CallFuncO were removed since it can be created with simulated with CallFuncN and CallFunc. See ActionsTest.cpp for more examples
  • MenuItem supports std::function<void(Node*)> as callbacks
强大枚举类型更新:
Examples:
v2.1v3.0
kCCTexture2DPixelFormat_RGBA8888Texture2D::PixelFormat::RGBA8888
kCCDirectorProjectionCustomDirector::Projection::CUSTOM
ccGREENColor3B::GREEN
CCPointZeroPoint::ZERO
CCSizeZeroSize::ZERO

The old values can still be used, but are not deprecated.


override
当虚函数被override关健字修饰时,子类实现时有override标记


    Removed Objective-C patterns

移除了所有Object-c模式,删除了CC前辍使用纯C++函数

对于类的使用改变:
2dx-3.0 两也不用使用using coco2d namespace

     clone() instead of copy()

clone() returns an autoreleased version of the copy.

copy() is no longer supported. If you use it, it will compile, but the code will crash.

Example:

1// v2.1
2CCMoveBy *action = (CCMoveBy*) move->copy();
3action->autorelease();
4
5// v3.0
6// No need to do autorelease, no need to do casting.
7auto action = move->clone();

     

     Singletons use getInstance() and destroyInstance()

All singletons use getInstance() and destroyInstance() (if applicable) to get and destroy the instance.

Examples:

v2.1v3.0
CCDirector->sharedDirector()Director->getInstance()
CCDirector->endDirector()Director->destroyInstance()
etc...

v2.1 methods are still available, but they were tagged as deprecated.


     getters

Getters now use the get prefix.

Examples:

v2.1v3.0
node->boundingBox()node->getBoundingBox()
sprite->nodeToParentTransform()sprite->getNodeToParentTransform()
etc...

And getters were also tagged as const in their declaration. Example:

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

v2.1 methods are still available, but they were tagged as deprecated.



     POD types

Methods that were receiving POD types as arguments (eg: TexParamsPointSize, etc.) are being passed as constreference.

Example:

1// v2.1
2void setTexParameters(ccTexParams* texParams);
3
4// v3.0
5void setTexParameters(const ccTexParams& texParams);

   Misc API Changes

  ccTypes.h

Remove cc prefix for structure names in ccTypes.h, move global functions into static member functions, and move global constants into const static member variables.

structure name before changingstructure name after changing
ccColor3BColor3B
ccColor4BColor4B
ccColor4FColor4F
ccVertex2FVertex2F
ccVertex3FVertex3F
ccTex2FTex2F
ccPointSpritePointSprite
ccQuad2Quad2
ccQuad3Quad3
ccV2F_C4B_T2FV2F_C4B_T2F
ccV2F_C4F_T2FV2F_C4F_T2F
ccV3F_C4B_T2FV3F_C4B_T2F
ccV2F_C4B_T2F_TriangleV2F_C4B_T2F_Triangle
ccV2F_C4B_T2F_QuadV2F_C4B_T2F_Quad
ccV3F_C4B_T2F_QuadV3F_C4B_T2F_Quad
ccV2F_C4F_T2F_QuadV2F_C4F_T2F_Quad
ccBlendFuncBlendFunc
ccT2F_QuadT2F_Quad
ccAnimationFrameDataAnimationFrameData

Global functions changed example

 1
 2// in v2.1
 3ccColor3B color3B = ccc3(0, 0, 0);
 4ccc3BEqual(color3B, ccc3(1, 1, 1));
 5ccColor4B color4B = ccc4(0, 0, 0, 0); 
 6ccColor4F color4F = ccc4f(0, 0, 0, 0);
 7color4F = ccc4FFromccc3B(color3B);
 8color4F = ccc4FFromccc4B(color4B);
 9ccc4FEqual(color4F, ccc4F(1, 1, 1, 1));
10color4B = ccc4BFromccc4F(color4F);
11
12color3B = ccWHITE;
13
14// in v3.0
15Color3B color3B = Color3B(0, 0, 0);
16color3B.equals(Color3B(1, 1, 1));
17Color4B color4B = Color4B(0, 0, 0, 0);
18Color4F color4F = Color4F(0, 0, 0, 0);
19color4F = Color4F(color3B);
20color4F = Color4F(color4B);
21color4F.equals(Color4F(1, 1, 1, 1));
22color4B = Color4B(color4F);
23
24color3B = Color3B::WHITE;

  deprecated functions and global variables

old namenew name
ccpPoint
ccpNegPoint::-
ccpAddPoint::+
ccpSubPoint::-
ccpMultPoint::*
ccpMidpointPoint::getMidpoint
ccpDotPoint::dot
ccpCrosssPoint::cross
ccpPerpPoint::getPerp
ccpRPerpPoint::getRPerp
ccpProjectPoint::project
ccpRotatePoint::rotate
ccpUnrotatePoint::unrotate
ccpLengthSQPoint::getLengthSq()
ccpDistanceSQPoint::getDistanceSq
ccpLengthPoint::getLength
ccpDistancePoint::getDistance
ccpNormalizePoint::normalize
ccpForAnglePoint::forAngle
ccpToAnglePoint::getAngle
ccpClampPoint::getClampPoint
ccpFromSizePoint::Point
ccpCompOpPoint::compOp
ccpLerpPoint::lerp
ccpFuzzyEqualPoint::fuzzyEqual
ccpCompMultPoint::Point
ccpAngleSignedPoint::getAngle
ccpAnglePoint::getAngle
ccpRotateByAnglePoint::rotateByAngle
ccpLineInersectPoint::isLineIntersect
ccpSegmentIntersectPoint::isSegmentIntersect
ccpIntersectPointPoint::getIntersectPoint
CCPointMakePoint::Point
CCSizeMakeSize::Size
CCRectMakeRect::Rect
PointZeroPoint::ZERO
SizeZeroSize::ZERO
RectZeroRect::ZERO
TiledGrid3DAction::tileTiledGrid3DAction::getTile
TiledGrid3DAction::originalTileTiledGrid3DAction::getOriginalTile
TiledGrid3D::tileTiledGrid3D::getTile
TiledGrid3D::originalTileTiledGrid3D::getOriginalTile
Grid3DAction::vertexGrid3DAction::getVertex
Grid3DAction::originalVertexGrid3DAction::getOriginalVertex
Grid3D::vertexGrid3D::getVertex
Grid3D::originalVertexGrid3D::getOriginalVertex
Configuration::sharedConfigurationConfiguration::getInstance
Configuration::purgeConfigurationConfiguration::destroyInstance()
Director::sharedDirector()Director::getInstance()
FileUtils::sharedFileUtilsFileUtils::getInstance
FileUtils::purgeFileUtilsFileUtils::destroyInstance
EGLView::sharedOpenGLViewEGLView::getInstance
ShaderCache::sharedShaderCacheShaderCache::getInstance
ShaderCache::purgeSharedShaderCacheShaderCache::destroyInstance
AnimationCache::sharedAnimationCacheAnimationCache::getInstance
AnimationCache::purgeSharedAnimationCacheAnimationCache::destroyInstance
SpriteFrameCache::sharedSpriteFrameCacheSpriteFrameCache::getInstance
SpriteFrameCache:: purgeSharedSpriteFrameCacheSpriteFrameCache::destroyInstance
NotificationCenter::sharedNotificationCenterNotificationCenter::getInstance
NotificationCenter:: purgeNotificationCenterNotificationCenter::destroyInstance
Profiler::sharedProfilerProfiler::getInstance
UserDefault::sharedUserDefaultUserDefault::getInstance
UserDefault::purgeSharedUserDefaultUserDefault::destroyInstance
Application::sharedApplicationApplication::getInstance
ccc3()Color3B()
ccc3BEqual()Color3B::equals()
ccc4()Color4B()
ccc4FFromccc3B()Color4F()
ccc4f()Color4F()
ccc4FFromccc4B()Color4F()
ccc4BFromccc4F()Color4B()
ccc4FEqual()Color4F::equals()
ccWHITEColor3B::WHITE
ccYELLOWColor3B::YELLOW
ccBLUEColor3B::BLUE
ccGREENColor3B::GREEN
ccREDColor3B::RED
ccMAGENTAColor3B::MAGENTA
ccBLACKColor3B::BLACK
ccORANGEColor3B::ORANGE
ccGRAYColor3B::GRAY
kBlendFuncDisableBlendFunc::BLEND_FUNC_DISABLE

--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------

(1)字符传参   使用  const std::string&   str;看实例:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //设置缓存批处理  
  2.     cocos2d::SpriteBatchNode*  setCache_batch(const  std::string& plist,const std::string& pvr);  


 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. SpriteBatchNode*  Base::setCache_batch(const std::string&  plist, const std::string&  pvr)  
  2. {  
  3.     auto  cache=SpriteFrameCache::getInstance();  
  4.     cache->addSpriteFramesWithFile(plist);  
  5.   
  6.   
  7.     auto  batchNode=SpriteBatchNode::create(pvr);  
  8.     return  batchNode;  
  9. }  

 

(2):for循环有点类似python脚本:看实例:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. const   std::string&   str =" ni  hao p";  
  2. for(auto  &t  :str)//从容器中引用对象  
  3. log("%c",str);  
  4.   
  5. 控制台打印:  
  6.   
  7. n  
  8. i  
  9.    
  10. h  
  11. a  
  12. o  
  13.    
  14. p  


 

(3):获取导演和缓存时,废除了share   改为 getInstance();  实例:

      CCDirector::getInstance(); 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. SpriteFrameCache::getInstance();  

 

(4):获取地图的图层时:废弃了laynamed("");改为  getLayer("");实例:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. _myMap=TMXTiledMap::create("map/allmap1.tmx");  
  2.     _myMap->setScale(1.7f);  
  3.     _myMap->getLayer("barry")->setVisible(false);  

 

(5):从精灵框帧缓存中创建动画时,废弃了原来的数组,改用向量来创建,大部分情况下还是使用数组

实例:


  有时间再补,睡觉.....




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值