cocos2d-x v2 升级到 v3

前端之家收集整理的这篇文章主要介绍了cocos2d-x v2 升级到 v3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

常用类名改变

删除了CC前缀
CCAction

CCAction -> Action
CCPoint -> Point
CCAnimation -> Animation
CCSprite -> Sprite
CCLabel -> Label
CCMenu -> Menu
CCObject -> Ref
CCNode -> Node
CCScene -> Scene
CCLayer -> Layer
CCSpriteBatchNoe -> SpriteBatchNode
CCTMXTiledMap -> TMXTiledMap
CCDictionary -> ValueMap
CCArray -> ValueVector
CCString -> Value

CCString 改变

之前:

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

之后:

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

CCDictionary 改变

之前:

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

之后:

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

CCArray 改变

改为标准的vector容器

CCArray* arr; -> ValueVector arr;
arr->addObject(sprite); -> arr.pushBack(sprite);
arr->removeObject(sprite); -> arr.eraSEObject(sprite);
arr->removeObjectAtIndex(i); -> arr.erase(i);
arr->objectAtIndex(i); -> arr.at(i);
arr->count(); -> arr.size();

触摸函数改变

ccTouchBegan -> onTouchBegan
ccTouchMoved -> onTouchMoved
ccTouchEnded -> onTouchEnded

ccMacros.h中宏定义

// new callbacks based on C++11
#define CC_CALLBACK_0(__selector__,__target__,...) std::bind(&__selector__,##__VA_ARGS__)
#define CC_CALLBACK_1(__selector__,std::placeholders::_1,##__VA_ARGS__)
#define CC_CALLBACK_2(__selector__,std::placeholders::_2,##__VA_ARGS__)
#define CC_CALLBACK_3(__selector__,std::placeholders::_3,##__VA_ARGS__)

v2调用方法

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

v3调用方法1

MenuItemImage *item1 = MenuItemImage::create(s_pPathB1,CC_CALLBACK_1(ActionsDemo::backCallback,this));

v3调用方法2

MenuItemImage *item1 = MenuItemImage::create(s_pPathB1,[=](Ref* sender){ //code here }););

全局单例类的改变

CCEGLView::sharedOpenGLView(); -> Director::getInstance()->getOpenGLView();
CCTextureCache::sharedTextureCache(); -> Director::getInstance()->getTextureCache();
CCNotificationCenter::sharedNotificationCenter(); -> Director::getInstance()->getEventDispatcher();
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
kBlendFuncDisable -> BlendFunc::BLEND_FUNC_DISABLE

CCTime 改变

cc_timeval -> timeval
CCTime::gettimeofdayCocos2d -> gettimeofday
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 变化

一般做游戏不太涉及得到

CCGLProgram -> GLProgram
kCCUniformPMatrix_s -> GLProgram::UNIFORM_NAME_P_MATRIX
kCCUniformMVMatrix_s -> GLProgram::UNIFORM_NAME_MV_MATRIX
kCCUniformMVPMatrix_s -> GLProgram::UNIFORM_NAME_MVP_MATRIX
kCCUniformTime_s -> GLProgram::UNIFORM_NAME_TIME
kCCUniformSinTime_s -> GLProgram::UNIFORM_NAME_SIN_TIME
kCCUniformCosTime_s -> GLProgram::UNIFORM_NAME_COS_TIME
kCCUniformRandom01_s -> GLProgram::UNIFORM_NAME_RANDOM01
kCCUniformSampler_s -> GLProgram::UNIFORM_NAME_SAMPLER0
kCCUniformAlphaTestValue -> GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE
kCCAttributeNameColor -> GLProgram::ATTRIBUTE_NAME_COLOR
kCCAttributeNamePosition -> GLProgram::ATTRIBUTE_NAME_POSITION
kCCAttributeNameTexCoord -> GLProgram::ATTRIBUTE_NAME_TEX_COORD

函数对照

点操作

ccp -> Vec2
点负数 ccpNeg -> Vect::negate or -
点相加 ccpAdd -> Vect::add or +
点相减 ccpSub -> Vect::subtract or -
点相乘 ccpMult -> Vect::*
点获得中点 ccpMidpoint -> Vect::getMidpoint
点积 ccpDot -> Vect::dot
叉积 ccpCrosss -> Vect::cross
逆时针旋转90度 ccpPerp -> Vect::getPerp
顺时针旋转90度 ccpRPerp -> Vect::getRPerp
投影 ccpProject -> Vect::project
ccpRotate -> Vect::rotate
ccpunrotate -> Vect::unrotate
长度未开方(点积) ccpLengthSQ -> Vect::getLengthSq
距离未开方 ccpDistanceSQ -> distanceSquared
长度 ccpLength -> getLength
距离 ccpDistance -> getDistance
取模 ccpNormalize -> normalize
从角度获得 ccpForAngle -> forAngle
转换为角度 ccpToAngle -> getAngle
ccpClamp -> getClampPoint
ccpFromSize ->
对x,y值进行函数处理 ccpCompOp -> compOp
两点构成直线上的点 ccpLerp -> lerp
模糊相等,两个点靠的足够近 ccpFuzzyEqual -> fuzzyEqual
ccpCompMult ->
和x轴的夹角或者和向量组成的夹角 ccpAngleSigned -> getAngle
同上 ccpAngle -> getAngle
旋转角度 ccpRotateByAngle -> rotateByAngle
线是否相交/四个点是否在一个平面 ccpLineInersect -> isLineIntersect
线段是否相交/四个点是否在一个平面 ccpSegmentIntersect -> isSegmentIntersect
获取线段交点 ccpIntersectPoint -> getIntersectPoint
CCPointMake ->

色彩操作

ccc3() -> Color3B()
ccc3BEqual() -> Color3B::equals()
ccc4() -> Color4B()
ccc4f() -> Color4F()
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
ccc4FFromccc3B ->
ccc4FFromccc4B ->
ccc4BFromccc4F ->

用宏定义来查找api变化

全局搜索 CC_DEPRECATED_ATTRIBUTE

参考:
cocos2d-x v2 和 v3 对照手册
升级到Cocos2d-x v3.3-RC0总结Director的主线解析
cocos2dx2.x&3.x部分函数对照表

猜你在找的Cocos2d-x相关文章