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

来源:互联网 发布:淘宝五金店铺销量如何 编辑:程序博客网 时间:2024/05/05 16:56

cocos2d-x v2 和 v3 对照手册

cocos2d-x 常用类名改变

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

#v2v31CCActionAction2CCPointPoint3CCAnimationAnimation4CCSpriteSprite5CCLabelLabel6CCMenuMenu7CCObjectRef8CCNodeNode9CCSceneScene10CCLayerLayer11CCSpriteBatchNoeSpriteBatchNode12CCTMXTiledMapTMXTiledMap

cocos2d-x 类名改变

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

#v2v31CCDictionaryValueMap2CCArrayValueVector3CCStringValue

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 容器的标准用法了。

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

触摸用法改变

#v2v31ccTouchBeganonTouchBegan2ccTouchMovedonTouchMoved3ccTouchEndedonTouchEnded

单例类用法改变

#v2v31CCEGLView::sharedOpenGLView();Director::getInstance()->getOpenGLView();2CCTextureCache::sharedTextureCache();Director::getInstance()->getTextureCache();3CCNotificationCenter::sharedNotificationCenter();Director::getInstance()->getEventDispatcher();

CCTime 用法改变

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

#v2v31cc_timevaltimeval2CCTime::gettimeofdayCocos2dgettimeofday3CCTime::timesubCocos2dgetTimeDiffenceMS

范例:

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 的用法变化

#v2v31CCGLProgramGLProgram3kCCUniformPMatrix_sGLProgram::UNIFORM_NAME_P_MATRIX4kCCUniformMVMatrix_sGLProgram::UNIFORM_NAME_MV_MATRIX5kCCUniformMVPMatrix_sGLProgram::UNIFORM_NAME_MVP_MATRIX6kCCUniformTime_sGLProgram::UNIFORM_NAME_TIME7kCCUniformSinTime_sGLProgram::UNIFORM_NAME_SIN_TIME8kCCUniformCosTime_sGLProgram::UNIFORM_NAME_COS_TIME9kCCUniformRandom01_sGLProgram::UNIFORM_NAME_RANDOM0110kCCUniformSampler_sGLProgram::UNIFORM_NAME_SAMPLER011kCCUniformAlphaTestValueGLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE12kCCAttributeNameColorGLProgram::ATTRIBUTE_NAME_COLOR13kCCAttributeNamePositionGLProgram::ATTRIBUTE_NAME_POSITION14kCCAttributeNameTexCoordGLProgram::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));
0 0