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

src:http://blog.csdn.net/u012234115/article/details/39717853

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

 

1.去CC

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

2.0
CCSprite  CCCallFunc CCNode ..
3.0
Sprite CallFunc Node ..

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
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
SpriteFrameCache::sharedSpriteFrameCache()
AnimationCache::sharedAnimationCache()
NotificationCenter::sharedNotificationCenter()


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

4.POD类别

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

2.0
void setPoint(CCPoint p)
3.0
void setPoint(const Point& p)

5.点触事件,

此部分全面更新采用Event Listener

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

bool FBMainScene::onTouchBegan(Touch *touch,Event *pEvent){
    CCLOG("onTouchBegan");
    Point point = this->convertToWorldSpace(this->convertTouchToNodeSpace(touch));
    return true;
}

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

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

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

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

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

下面是另一个写法

[cpp] view plaincopy

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

  2. ...    

  3. auto listener = EventListenerTouchOneByOne::create();    

  4. listener->setSwallowTouch(true);    

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

  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. // The priority of the touch listener is based on the draw order of sprite    

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

  11. // Or the priority of the touch listener is a fixed value    

  12. EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 100); // 100 is a fixed value    

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

6.CC_CALLBACK_*

CC_CALLBACK_0 CC_CALLBACK_1 CC_CALLBACK_2 CC_CALLBACK_3

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

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

// new callbacks based on C++11
#define CC_CALLBACK_0(__selector__,__target__, ) std::bind(&__selector__,__target__, ##__VA_ARGS__)
#define CC_CALLBACK_1(__selector__,__target__, ) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
#define CC_CALLBACK_2(__selector__,__target__, ) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
#define CC_CALLBACK_3(__selector__,__target__, ) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, 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.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 |
// in v2.1ccColor3B color3B = ccc3(0, 0, 0);ccc3BEqual(color3B, ccc3(1, 1, 1));ccColor4B color4B = ccc4(0, 0, 0, 0);ccColor4F color4F = ccc4f(0, 0, 0, 0);color4F = ccc4FFromccc3B(color3B);color4F = ccc4FFromccc4B(color4B);ccc4FEqual(color4F, ccc4F(1, 1, 1, 1));color4B = ccc4BFromccc4F(color4F);color3B = ccWHITE;// in v3.0Color3B color3B = Color3B(0, 0, 0);color3B.equals(Color3B(1, 1, 1));Color4B color4B = Color4B(0, 0, 0, 0);Color4F color4F = Color4F(0, 0, 0, 0);color4F = Color4F(color3B);color4F = Color4F(color4B);color4F.equals(Color4F(1, 1, 1, 1));color4B = Color4B(color4F);color3B = Color3B::WHITE;
| 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 |

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

你可能感兴趣的:(cocos2dx,cocos2dx,改变,3.x,2.x)