cocos2d-x v3.0新特性及使用

来源:互联网 发布:淘宝快递打单软件 编辑:程序博客网 时间:2024/05/01 06:00

转自:http://blog.csdn.net/inspironx/article/details/9850523


八月份cocos2d-x官网发布了v3.0版本,这次更新的内容特别多,包括2dx的架构以及使用

总得来说,给开发者带来了很大的便利:


运行环境需求:

  • 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.0kCCTexture2DPixelFormat_RGBA8888Texture2D::PixelFormat::RGBA8888kCCDirectorProjectionCustomDirector::Projection::CUSTOMccGREENColor3B::GREENCCPointZeroPoint::ZEROCCSizeZeroSize::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.12CCMoveBy *action = (CCMoveBy*) move->copy();3action->autorelease();45// v3.06// 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.0CCDirector->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.0node->boundingBox()node->getBoundingBox()sprite->nodeToParentTransform()sprite->getNodeToParentTransform()etc...

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

1// v2.12virtual float getScale();34// v3.05virtual 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.12void setTexParameters(ccTexParams* texParams);34// v3.05void 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 changingccColor3BColor3BccColor4BColor4BccColor4FColor4FccVertex2FVertex2FccVertex3FVertex3FccTex2FTex2FccPointSpritePointSpriteccQuad2Quad2ccQuad3Quad3ccV2F_C4B_T2FV2F_C4B_T2FccV2F_C4F_T2FV2F_C4F_T2FccV3F_C4B_T2FV3F_C4B_T2FccV2F_C4B_T2F_TriangleV2F_C4B_T2F_TriangleccV2F_C4B_T2F_QuadV2F_C4B_T2F_QuadccV3F_C4B_T2F_QuadV3F_C4B_T2F_QuadccV2F_C4F_T2F_QuadV2F_C4F_T2F_QuadccBlendFuncBlendFuncccT2F_QuadT2F_QuadccAnimationFrameDataAnimationFrameData

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);1112color3B = ccWHITE;1314// in v3.015Color3B 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);2324color3B = Color3B::WHITE;

  deprecated functions and global variables

old namenew nameccpPointccpNegPoint::-ccpAddPoint::+ccpSubPoint::-ccpMultPoint::*ccpMidpointPoint::getMidpointccpDotPoint::dotccpCrosssPoint::crossccpPerpPoint::getPerpccpRPerpPoint::getRPerpccpProjectPoint::projectccpRotatePoint::rotateccpUnrotatePoint::unrotateccpLengthSQPoint::getLengthSq()ccpDistanceSQPoint::getDistanceSqccpLengthPoint::getLengthccpDistancePoint::getDistanceccpNormalizePoint::normalizeccpForAnglePoint::forAngleccpToAnglePoint::getAngleccpClampPoint::getClampPointccpFromSizePoint::PointccpCompOpPoint::compOpccpLerpPoint::lerpccpFuzzyEqualPoint::fuzzyEqualccpCompMultPoint::PointccpAngleSignedPoint::getAngleccpAnglePoint::getAngleccpRotateByAnglePoint::rotateByAngleccpLineInersectPoint::isLineIntersectccpSegmentIntersectPoint::isSegmentIntersectccpIntersectPointPoint::getIntersectPointCCPointMakePoint::PointCCSizeMakeSize::SizeCCRectMakeRect::RectPointZeroPoint::ZEROSizeZeroSize::ZERORectZeroRect::ZEROTiledGrid3DAction::tileTiledGrid3DAction::getTileTiledGrid3DAction::originalTileTiledGrid3DAction::getOriginalTileTiledGrid3D::tileTiledGrid3D::getTileTiledGrid3D::originalTileTiledGrid3D::getOriginalTileGrid3DAction::vertexGrid3DAction::getVertexGrid3DAction::originalVertexGrid3DAction::getOriginalVertexGrid3D::vertexGrid3D::getVertexGrid3D::originalVertexGrid3D::getOriginalVertexConfiguration::sharedConfigurationConfiguration::getInstanceConfiguration::purgeConfigurationConfiguration::destroyInstance()Director::sharedDirector()Director::getInstance()FileUtils::sharedFileUtilsFileUtils::getInstanceFileUtils::purgeFileUtilsFileUtils::destroyInstanceEGLView::sharedOpenGLViewEGLView::getInstanceShaderCache::sharedShaderCacheShaderCache::getInstanceShaderCache::purgeSharedShaderCacheShaderCache::destroyInstanceAnimationCache::sharedAnimationCacheAnimationCache::getInstanceAnimationCache::purgeSharedAnimationCacheAnimationCache::destroyInstanceSpriteFrameCache::sharedSpriteFrameCacheSpriteFrameCache::getInstanceSpriteFrameCache:: purgeSharedSpriteFrameCacheSpriteFrameCache::destroyInstanceNotificationCenter::sharedNotificationCenterNotificationCenter::getInstanceNotificationCenter:: purgeNotificationCenterNotificationCenter::destroyInstanceProfiler::sharedProfilerProfiler::getInstanceUserDefault::sharedUserDefaultUserDefault::getInstanceUserDefault::purgeSharedUserDefaultUserDefault::destroyInstanceApplication::sharedApplicationApplication::getInstanceccc3()Color3B()ccc3BEqual()Color3B::equals()ccc4()Color4B()ccc4FFromccc3B()Color4F()ccc4f()Color4F()ccc4FFromccc4B()Color4F()ccc4BFromccc4F()Color4B()ccc4FEqual()Color4F::equals()ccWHITEColor3B::WHITEccYELLOWColor3B::YELLOWccBLUEColor3B::BLUEccGREENColor3B::GREENccREDColor3B::REDccMAGENTAColor3B::MAGENTAccBLACKColor3B::BLACKccORANGEColor3B::ORANGEccGRAYColor3B::GRAYkBlendFuncDisableBlendFunc::BLEND_FUNC_DISABLE

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

创建新项目:
 v3.0不再需要创建Xcode模版,使用create-multi-platform-projects.py来创建一个跨平台的项目,使用方法
 1,cd 到2dx根目录,MAC平台使用./create-multi-platform-projects.py  然后提示:
-bash: ./create-multi-platform-projects.py: /usr/bin/evn: bad interpreter: No such file or directory
没关系,我们打开.py文件,发现其实引用的是tools/project_creator/create_project.py 这文件
 2,我们再cd 到project_creator文件中,使用 ./create_project.py -p <PROJECT_NAME> -k <PACKAGE_NAME> -l <cpp|lua|javascript>
 3,这样我们就可以在2dx的projects文件夹中找到创建好的项目

关于项目的选择:
图示:

红包点选就可以选择对应运行平台

原创粉丝点击