不得不说,cocos2d-x的版本更迭真是快,而最新的cocos2d-x v3.0 Beta版本中包含了许多新的特性和与之前2.x版本不同的内容,虽然说,目前3.0只是beta版本,但目前很多游戏已经采用这个3.0版本了,估计3.0的最终版本也会很快推出。
从创建项目开始了解一下这个最新的3.0版本的一些新特性吧!
一、创建项目(针对在mac os x中进行开发)
在前面我有一篇文章 点击打开链接 已经介绍过在 cocos2dx 2.2 版本中创建项目只能是在终端中进行,对于习惯了在xcode中直接通过项目模板生成项目的来说,这样的方式的确是有点不太方便。
但是在cocos2d-x v3.0 Beta版本中,采用了一种比较人性化图形界面创建方式。
在这里 点击打开链接 有详细的新版本使用介绍。
下面我实践一下如何创建一个 cocos2dx for ios & osx 的项目。(有两种方式)
1、第一种,当然是延续了2.2版本之后的直接命令行创建,这个具体方法可以请参考我之前的文章。点击打开链接
Example:
$ cd cocos2d-x/tools/project-creator
$ ./project-creator.py -n mygame -k com.your_company.mygame -l cpp -p /home/mygame
$ cd /home/mygame
Feature added in v3.0-pre-alpha0
A subset of C++11 features are being used in cocos2d-x:
std::function
, including lambda objects for callbacksstd::thread
for threadingoverride
context keyword, for overriden methodsCallFunc
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 examplesMenuItem
supports std::function<void(Node*)>
as callbacks CallFunc
example:
MenuItem
example:
Feature added in v3.0-pre-alpha0
Constants and enums that started with k
, and that usually were defined as int
or as simple enum
where replaced with strongly typed enums ( enum class
) to prevent collisions and type errors. The new format is:
| v2.1 | v3.0 |
| kTypeValue | Type::VALUE |
Examples:
| v2.1 | v3.0 |
| kCCTexture2DPixelFormat_RGBA8888 | Texture2D::PixelFormat::RGBA8888 |
| kCCDirectorProjectionCustom | Director::Projection::CUSTOM |
| ccGREEN | Color3B::GREEN |
| CCPointZero | Point::ZERO |
| CCSizeZero | Size::ZERO |
The old values can still be used, but are not deprecated.
To catch possible errors while overriding methods, subclasses with override methods have the override
context keyword. Example:
Feature added in v3.0-pre-alpha0
Changes in classes
Since cocos2d-x already uses the cocos2d
namespace, there is not need to add the prefix CC
to all its classes.
Examples:
| v2.1 | v3.0 |
| CCSprite | Sprite |
| CCNode | Node |
| CCDirector | Director |
| etc... |
v2.1 class names are still available, but they were tagged as deprecated.
Changes in free functions
For the drawing primitives:
DrawPrimitives
namespacecc
prefix was removedFor the gl proxy functions:
GL
namespaceccGL
prefix was removedExamples:
| v2.1 | v3.0 |
| ccDrawPoint() | DrawPrimitives::drawPoint() |
| ccDrawCircle() | DrawPrimitives::drawCircle() |
| ccGLBlendFunc() | GL::blendFunc() |
| ccGLBindTexture2D() | GL::bindTexture2D() |
| etc... |
v2.1 free functions are still available, but they were tagged as deprecated.
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:
All singletons use getInstance()
and destroyInstance()
(if applicable) to get and destroy the instance.
Examples:
| v2.1 | v3.0 |
| CCDirector->sharedDirector() | Director->getInstance() |
| CCDirector->endDirector() | Director->destroyInstance() |
| etc... |
v2.1 methods are still available, but they were tagged as deprecated.
Getters now use the get
prefix.
Examples:
| v2.1 | v3.0* |
| node->boundingBox() | node->getBoundingBox() |
| sprite->nodeToParentTransform() | sprite->getNodeToParentTransform() |
| etc... |
And getters were also tagged as const
in their declaration. Example:
v2.1 methods are still available, but they were tagged as deprecated.
Methods that were receiving POD types as arguments (eg: TexParams
, Point
, Size
, etc.) are being passed as const
reference.
Example:
Feature added in v3.0-beta
The renderer functionality has been decoupled from the Scene graph / Node logic. A new object called Renderer
is responsible for rendering the object.
Auto-batching and auto-culling support has been added.
Please, see this document for detail information about its internal funcitonality:https://docs.google.com/document/d/17zjC55vbP_PYTftTZEuvqXuMb9PbYNxRFu0EGTULPK8/edit
Feature added in v3.0-alpha0
Feature added in v3.0-alpha0
All events like touch event, keyboard event, acceleration event and custom event are dispatched by EventDispatcher
.TouchDispatcher
, KeypadDispatcher
, KeyboardDispatcher
, AccelerometerDispatcher
were removed.
For TouchOneByOne:
For TouchAllAtOnce
Feature added in v3.0-pre-alpha0
Physics integration have five concepts: PhysicsWorld
, PhysicsBody
, PhysicsShape
, PhysicsJoint
and PhysicsContact
. You must define CC_USE_PHYSICS
macro in ccConfig.h
to use the physics API.
A PhysicsWorld
object simulates collisions and other physical properties, you do not create it directly, you can get it from scene which create with physics.
A PhysicsBody
object is used to add physics simulation to a node. If you create a PhysicsBody
and set it to a node, and add the node the a scene which create with physics, it will perform the physics simulation when update.
A PhysicsShape
object is a shape that make the body can have collisions. you can add one or more PhysicsShape
to aPhysicsBody
. Shape classes: PhysicsShapeCircle
, PhysicsShapeBox
, PhysicsShapePolygon
, PhysicsShapeEdgeSegment
,PhysicsShapeEdgeBox
, PhysicsShapeEdgePolygon
, PhysicsShapeEdgeChain
.
A PhysicsJoint
object connects two physics bodies together so that they are simulated together by the physics world. Joint classes: PhysicsJointFixed
, PhysicsJointLimit
, PhysicsJointPin
, PhysicsJointDistance
, PhysicsJointSpring
,PhysicsJointGroove
, PhysicsJointRotarySpring
, PhysicsJointRotaryLimit
, PhysicsJointRatchet
, PhysicsJointGear
,PhysicsJointMotor
.
A PhysicsContact
object is created automatically to describes a contact between two physical bodies in a PhysicsWorld
. you can control the contact behavior from the physics contact event listener. Other classes contain the contact information:PhysicsContactPreSolve
, PhysicsContactPostSolve
. The event listener for physics: EventListenerPhysicsContact
,EventListenerPhysicsContactWithBodies
, EventListenerPhysicsContactWithShapes
, EventListenerPhysicsContactWithGroup
.
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.
| v2.1 struct names | v3.0 struct names |
| ccColor3B | Color3B |
| ccColor4B | Color4B |
| ccColor4F | Color4F |
| ccVertex2F | Vertex2F |
| ccVertex3F | Vertex3F |
| ccTex2F | Tex2F |
| ccPointSprite | PointSprite |
| ccQuad2 | Quad2 |
| ccQuad3 | Quad3 |
| ccV2F_C4B_T2F | V2F_C4B_T2F |
| ccV2F_C4F_T2F | V2F_C4F_T2F |
| ccV3F_C4B_T2F | V3F_C4B_T2F |
| ccV2F_C4B_T2F_Triangle | V2F_C4B_T2F_Triangle |
| ccV2F_C4B_T2F_Quad | V2F_C4B_T2F_Quad |
| ccV3F_C4B_T2F_Quad | V3F_C4B_T2F_Quad |
| ccV2F_C4F_T2F_Quad | V2F_C4F_T2F_Quad |
| ccBlendFunc | BlendFunc |
| ccT2F_Quad | T2F_Quad |
| ccAnimationFrameData | AnimationFrameData |
Global functions changed example
| v2.1 names | v3.0 names |
| ccp | Point |
| ccpNeg | Point::- |
| ccpAdd | Point::+ |
| ccpSub | Point::- |
| ccpMult | Point::* |
| ccpMidpoint | Point::getMidpoint |
| ccpDot | Point::dot |
| ccpCrosss | Point::cross |
| ccpPerp | Point::getPerp |
| ccpRPerp | Point::getRPerp |
| ccpProject | Point::project |
| ccpRotate | Point::rotate |
| ccpUnrotate | Point::unrotate |
| ccpLengthSQ | Point::getLengthSq() |
| ccpDistanceSQ | Point::getDistanceSq |
| ccpLength | Point::getLength |
| ccpDistance | Point::getDistance |
| ccpNormalize | Point::normalize |
| ccpForAngle | Point::forAngle |
| ccpToAngle | Point::getAngle |
| ccpClamp | Point::getClampPoint |
| ccpFromSize | Point::Point |
| ccpCompOp | Point::compOp |
| ccpLerp | Point::lerp |
| ccpFuzzyEqual | Point::fuzzyEqual |
| ccpCompMult | Point::Point |
| ccpAngleSigned | Point::getAngle |
| ccpAngle | Point::getAngle |
| ccpRotateByAngle | Point::rotateByAngle |
| ccpLineInersect | Point::isLineIntersect |
| ccpSegmentIntersect | Point::isSegmentIntersect |
| ccpIntersectPoint | Point::getIntersectPoint |
| CCPointMake | Point::Point |
| CCSizeMake | Size::Size |
| CCRectMake | Rect::Rect |
| PointZero | Point::ZERO |
| SizeZero | Size::ZERO |
| RectZero | Rect::ZERO |
| TiledGrid3DAction::tile | TiledGrid3DAction::getTile |
| TiledGrid3DAction::originalTile | TiledGrid3DAction::getOriginalTile |
| TiledGrid3D::tile | TiledGrid3D::getTile |
| TiledGrid3D::originalTile | TiledGrid3D::getOriginalTile |
| Grid3DAction::vertex | Grid3DAction::getVertex |
| Grid3DAction::originalVertex | Grid3DAction::getOriginalVertex |
| Grid3D::vertex | Grid3D::getVertex |
| Grid3D::originalVertex | Grid3D::getOriginalVertex |
| Configuration::sharedConfiguration | Configuration::getInstance |
| Configuration::purgeConfiguration | Configuration::destroyInstance() |
| Director::sharedDirector() | Director::getInstance() |
| FileUtils::sharedFileUtils | FileUtils::getInstance |
| FileUtils::purgeFileUtils | FileUtils::destroyInstance |
| EGLView::sharedOpenGLView | EGLView::getInstance |
| ShaderCache::sharedShaderCache | ShaderCache::getInstance |
| ShaderCache::purgeSharedShaderCache | ShaderCache::destroyInstance |
| AnimationCache::sharedAnimationCache | AnimationCache::getInstance |
| AnimationCache::purgeSharedAnimationCache | AnimationCache::destroyInstance |
| SpriteFrameCache::sharedSpriteFrameCache | SpriteFrameCache::getInstance |
| SpriteFrameCache:: purgeSharedSpriteFrameCache | SpriteFrameCache::destroyInstance |
| NotificationCenter::sharedNotificationCenter | NotificationCenter::getInstance |
| NotificationCenter:: purgeNotificationCenter | NotificationCenter::destroyInstance |
| Profiler::sharedProfiler | Profiler::getInstance |
| UserDefault::sharedUserDefault | UserDefault::getInstance |
| UserDefault::purgeSharedUserDefault | UserDefault::destroyInstance |
| Application::sharedApplication | Application::getInstance |
| ccc3() | Color3B() |
| ccc3BEqual() | Color3B::equals() |
| ccc4() | Color4B() |
| ccc4FFromccc3B() | Color4F() |
| ccc4f() | Color4F() |
| ccc4FFromccc4B() | Color4F() |
| ccc4BFromccc4F() | Color4B() |
| ccc4FEqual() | Color4F::equals() |
| ccWHITE | Color3B::WHITE |
| ccYELLOW | Color3B::YELLOW |
| ccBLUE | Color3B::BLUE |
| ccGREEN | Color3B::GREEN |
| ccRED | Color3B::RED |
| ccMAGENTA | Color3B::MAGENTA |
| ccBLACK | Color3B::BLACK |
| ccORANGE | Color3B::ORANGE |
| ccGRAY | Color3B::GRAY |
| kBlendFuncDisable | BlendFunc::BLEND_FUNC_DISABLE |
Only configurating the *.ini files in the tools/tolua folder,not to write a lot of *.pkg files
When we want to add register and unregister functions of Lua function for class, we need to change the declarative and defined files and then bind to Lua. In v3.0, we use the ScriptHandlerMgr
. As an example, lets see the MenuItem
class: In the 2.x version, we needed to add a declaration in the MenuItem header file:
then implement them in the .cpp file. In the Lua script ,we use it as follow:
In v3.0 version, we only need to add the HandlerType
enum in the ScriptHandlerMgr
, and the implementation in luascript as follow:
Add a lot of deprecate funtions、table and classes to support 2.x version as far as possible Note:Rect does not support the origin and size member variables
Point、Size、Rect、Color3b、Color4b、Color4F、AffineTransform、FontDefinition、Array、Dictionary、PointArray are not bound. The difference is as follow:
Global functions about these classes are changed as follow:
Through the funtions of the LuaBasicConversion file,they can be converted the Lua table when they are as a parameter in the bindings generator.
You can find all the known issues "here":http://www.cocos2d-x.org/projects/native/issues