cocos2dx 2.x到cocos2dx 3.x的一些改变

src:http://www.jb51.cc/article/p-gvqsfzll-r.html

cocos2dx 3.x之后用的都是c++ 11的新语法,另外在引擎源码里面有很多地方作出了改变。

1.去CC

之前2.0的CC**,把CC都去掉,基本的元素都是保留的

2.0
CCSpriteCCCallFuncCCNode..
3.0
SpriteCallFuncNode..

2.cc***结构体改变

2.0
ccp(x,y)
ccpAdd(p1,p2)
ccpSub
ccpMult
ccpLength(p)
ccpDot(p1,p2);
ccc3()
ccc4()
ccWHITE
CCPointZero
CCSizeZero



2.0
Point(x,y)
p1+p2;
p1-p2
p1*p2
p.getLength()
p1.dot(p2)
Color3B()
Color4B()
Color3B::WHITE
Point::ZERO
Size:ZERO

3.shared单例对象改变

2.0
CCSizewinSize=CCDirector::sharedDirector()->getWinSize();
SpriteFrameCache::sharedSpriteFrameCache()
AnimationCache::sharedAnimationCache()
NotificationCenter::sharedNotificationCenter()


3.0
Sizesize=Director::getInstance()->getWinSize();
SpriteFrameCache::getInstance()
AnimationCache::getInstance()
NotificationCenter::getInstance()

4.POD类别

使用const为Point,Size,Rect进行常量修饰

2.0
voidsetPoint(CCPointp)
3.0
voidsetPoint(constPoint&p)

5.点触事件,

此部分全面更新采用Event Listener

autodispatcher=Director::getInstance()->getEventDispatcher();
autotouchListener=EventListenerTouchOneByOne::create();
touchListener->onTouchBegan=CC_CALLBACK_2(FBMainScene::onTouchBegan,this);
touchListener->onTouchMoved=CC_CALLBACK_2(FBMainScene::onTouchMoved,255);">this);
touchListener->onTouchEnded=CC_CALLBACK_2(FBMainScene::onTouchEnded,255);">this);
dispatcher->addEventListenerWithSceneGraphPriority(touchListener,255);">this);

boolFBMainScene::onTouchBegan(Touch*touch,Event*pEvent){
CCLOG("onTouchBegan");
Pointpoint=this->convertToWorldSpace(this->convertTouchToNodeSpace(touch));
returntrue;
}

voidFBMainScene::onTouchMoved(Touch*touch,Event*pEvent){
CCLOG("onTouchMoved");
}

voidFBMainScene::onTouchEnded(Touch*touch,Event*pEvent){
CCLOG("onTouchEnded");
}

//获得触点的方法也发生了改变:
Pointpoint=this->convertTouchToNodeSpace(touch));

//dispatcher控制方法:
dispatcher->addEventListener…
dispatcher->removeEventListener(listener);
dispatcher->removeAllListeners();

新的触摸机制是重大变化,可以查看其详细解释

下面是另一个写法

[cpp]view plaincopy

  1. autosprite=Sprite::create("file.png");

  2. ...

  3. autolistener=EventListenerTouchOneByOne::create();

  4. listener->setSwallowTouch(true);

  5. listener->onTouchBegan=[](Touch*touch,Event*event){do_some_thing();returntrue;};

  6. listener->onTouchMoved=[](Touch*touch,Event*event){do_some_thing();};

  7. listener->onTouchEnded=[](Touch*touch,Event*event){do_some_thing();};

  8. listener->onTouchCancelled=[](Touch*touch,Event*event){do_some_thing();};

  9. //Thepriorityofthetouchlistenerisbasedonthedraworderofsprite

  10. EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener,sprite);

  11. //Orthepriorityofthetouchlistenerisafixedvalue

  12. EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener,100);//100isafixedvalue

可以发现里面采用了lamda表达式

6.CC_CALLBACK_*

CC_CALLBACK_0 CC_CALLBACK_1 CC_CALLBACK_2 CC_CALLBACK_3

回调函数,分别携带不同的参数,方便

2.0
CCMenuItemFont*item=CCMenuItemFont::create("返回上个场景",255);">this,menu_selector(GameScene::backScene));
3.0
MenuItemFont*item=MenuItemLabel::create("返回上个场景",CC_CALLBACK_1(GameScene::backScene,255);">this));

//newcallbacksbasedonC++11
#defineCC_CALLBACK_0(__selector__,__target__,

)std::bind(&__selector__,##__VA_ARGS__)
#defineCC_CALLBACK_1(__selector__,std::placeholders::_1,255);">#defineCC_CALLBACK_2(__selector__,std::placeholders::_2,255);">#defineCC_CALLBACK_3(__selector__,std::placeholders::_3##__VA_ARGS__)

7.使用"Function"对象

CallFunc::create([&](){
Sprite*sprite=Sprite::create("s");
this->addChild(sprite);
});

这不就是lamda表达式么

8.使用clone代替copy

2.0
CCMoveBy*action=(CCMoveBy*)move->copy();
action->autorelease();
3.0
action=move->clone();
不需要autorelease,在clone已经实现。

9.Physics Integration 物理引擎

暂无使用,box2d 在 3.0中可以延续使用

在3.0的Physics中需要定义 PhysicsWorld,PhysicsBody,PhysicsShape,PhysicsJoint 等,于box2d相仿,使用前需要定义CC_USE_PHYSICS

……继续等待补充


一些变量名以及常量名的改变:

|v2.1structnames|v3.0structnames|
|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|
//inv2.1ccColor3Bcolor3B=ccc3(0,0);ccc3BEqual(color3B,ccc3(1,1,1));ccColor4Bcolor4B=ccc4(0,0);ccColor4Fcolor4F=ccc4f(0,0);color4F=ccc4FFromccc3B(color3B);color4F=ccc4FFromccc4B(color4B);ccc4FEqual(color4F,ccc4F(1,1));color4B=ccc4BFromccc4F(color4F);color3B=ccWHITE;//inv3.0Color3Bcolor3B=Color3B(0,0);color3B.equals(Color3B(1,1));Color4Bcolor4B=Color4B(0,0);Color4Fcolor4F=Color4F(0,0);color4F=Color4F(color3B);color4F=Color4F(color4B);color4F.equals(Color4F(1,1));color4B=Color4B(color4F);color3B=Color3B::WHITE;
|v2.1names|v3.0names|
|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|

未完待续~(虽然我很懒)

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...