cocos2dx 2.x与cocos2dx 3.x类库用法对比

cocos2d-x v2 和 v3 对照手册

cocos2d-x 常用类名改变

下面的表格中的类名的转换方式主要是直接删除了 CC 前缀。

# v2 v3
1 CCAction Action
2 CCPoint Point
3 CCAnimation Animation
4 CCSprite Sprite
5 CCLabel Label
6 CCMenu Menu
7 CCObject Ref
8 CCNode Node
9 CCScene Scene
10 CCLayer Layer
11 CCSpriteBatchNoe SpriteBatchNode
12 CCTMXTiledMap TMXTiledMap

cocos2d-x 类名改变

下面表格中的类名的转换就比较大了。

# v2 v3
1 CCDictionary ValueMap
2 CCArray ValueVector
3 CCString Value

CCString 用法改变

之前:

CCString* str = CCString::createWithFormat("%s.png","picture");

现在:

std::string str = StringUtils::format("%s.png","picture");

CCDictinoary 用法改变

之前:

CCDictionary* dict = CCDictionary::createWithContentsOfFile("name.plist");
CCArray* arr = (CCArray*) data->objectForKey("Levels");

现在:

std::string path = FileUtils::getInstance()->fullPathForFilename("name.plist");
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(path);
ValueVector arrLevels = data.at("Levels").asValueVector();

CCArray 用法改变

这里就是 C++ vector 容器的标准用法了。

# v2 v3
1 CCArray* sprites; Vector sprites;
2 sprites->addObject(sprite); sprites.pushBack(sprite);
3 sprites->removeObject(sprite); sprites.eraseObject(sprite);
4 sprites->removeObjectAtIndex(i); sprites.erase(i);
5 sprites->objectAtIndex(i); sprites.at(i);
6 sprites->count(); sprites.size();

触摸用法改变

# v2 v3
1 ccTouchBegan onTouchBegan
2 ccTouchMoved onTouchMoved
3 ccTouchEnded onTouchEnded

单例类用法改变

# v2 v3
1 CCEGLView::sharedOpenGLView(); Director::getInstance()->getOpenGLView();
2 CCTextureCache::sharedTextureCache(); Director::getInstance()->getTextureCache();
3 CCNotificationCenter::sharedNotificationCenter(); Director::getInstance()->getEventDispatcher();

CCTime 用法改变

CCTime cocos2d-x v3 中已经被删除了。

# v2 v3
1 cc_timeval timeval
2 CCTime::gettimeofdayCocos2d gettimeofday
3 CCTime::timesubCocos2d getTimeDiffenceMS

范例:

static inline float getTimeDifferenceMS(timeval& start, timeval& end)
{
    return ((((end.tv_sec - start.tv_sec)*1000.0f + end.tv_usec) - start.tv_usec) / 1000.0f);
}

OpenGL 的用法变化

# v2 v3
1 CCGLProgram GLProgram
3 kCCUniformPMatrix_s GLProgram::UNIFORM_NAME_P_MATRIX
4 kCCUniformMVMatrix_s GLProgram::UNIFORM_NAME_MV_MATRIX
5 kCCUniformMVPMatrix_s GLProgram::UNIFORM_NAME_MVP_MATRIX
6 kCCUniformTime_s GLProgram::UNIFORM_NAME_TIME
7 kCCUniformSinTime_s GLProgram::UNIFORM_NAME_SIN_TIME
8 kCCUniformCosTime_s GLProgram::UNIFORM_NAME_COS_TIME
9 kCCUniformRandom01_s GLProgram::UNIFORM_NAME_RANDOM01
10 kCCUniformSampler_s GLProgram::UNIFORM_NAME_SAMPLER0
11 kCCUniformAlphaTestValue GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE
12 kCCAttributeNameColor GLProgram::ATTRIBUTE_NAME_COLOR
13 kCCAttributeNamePosition GLProgram::ATTRIBUTE_NAME_POSITION
14 kCCAttributeNameTexCoord GLProgram::ATTRIBUTE_NAME_TEX_COORD

MenuItem 的用法变化

在 cocos2d-x v3 中,使用的是 C++11 提供的标准的 std::bind 功能来实现回调。

让我们看看 base/ccMacros.h 中的几个宏:

// 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__)

所以,对于 cocos2d-x v2 中这样的调用:

CMenuItemImage *item1 = CCMenuItemImage::create(s_pPathB1, s_pPathB2, this, menu_selector(ActionsDemo::backCallback));

在 cocos2d-x v3 中应该是这样的:

MenuItemImage *item1 = MenuItemImage::create(s_pPathB1, s_pPathB2, CC_CALLBACK_1(ActionsDemo::backCallback, this));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值