Cocos2d-x 2.x 升级为 3.x 常见变化纪录
1.去CC
之前2.0的CC**,把CC都去掉,主要的元素都是保留的
2.0
CCSprite CCCallFunc CCNode ..
3.0
Sprite CallFunc Node ..
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

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

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

3.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()
…
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)
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();
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();
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__)
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__,

#define CC_CALLBACK_1(__selector__,__target__,

#define CC_CALLBACK_2(__selector__,__target__,

#define CC_CALLBACK_3(__selector__,__target__,

7.使用"Function"对象
CallFunc::create([&](){
Sprite *sprite = Sprite::create("s");
this->addChild(sprite);
});
Sprite *sprite = Sprite::create("s");
this->addChild(sprite);
});
8.使用clone取代copy
2.0
CCMoveBy *action = (CCMoveBy*) move->copy();
action->autorelease();
3.0
action = move->clone();
不须要autorelease,在clone已经实现。
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
……继续等待补充
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)