cocos2d-x v2 和 v3 对照手册

本文转载自 zrong’s blog
原文地址:http://zengrong.net/post/2193.htm

2015-05-04 更新: 加入 OpenGL 和 MenuItem 的相关变化。


本文的内容来自于对其它几篇文章的翻译、修改和合成,同时,我也会不断增加自己的内容。

下面这部分内容来自对这篇文章的翻译:cocos2d-x v2 to v3 mapping guide

但这篇文章有一些老了,还有一些内容已经在 cocos2d-x 3.3 中过时。因此,我并没有进行完全对照翻译。对原文中的错误,我也进行了一些修改。


我的新项目开始使用 cocos2d-x v3 。cocos2d-x v3 和 v2 相比有非常大的改变。我把踩过的坑列在下面,以方便后来之人。

cocos2d-x 常用类名改变

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

#v2v3
1CCActionAction
2CCPointPoint
3CCAnimationAnimation
4CCSpriteSprite
5CCLabelLabel
6CCMenuMenu
7CCObjectRef
8CCNodeNode
9CCSceneScene
10CCLayerLayer
11CCSpriteBatchNoeSpriteBatchNode
12CCTMXTiledMapTMXTiledMap

cocos2d-x 类名改变

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

#v2v3
1CCDictionaryValueMap
2CCArrayValueVector
3CCStringValue

CCString 用法改变

之前:

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

现在:

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

CCDictinoary 用法改变

之前:

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

现在:

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

CCArray 用法改变

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

#v2v3
1CCArray* sprites;Vector

触摸用法改变

#v2v3
1ccTouchBeganonTouchBegan
2ccTouchMovedonTouchMoved
3ccTouchEndedonTouchEnded

单例类用法改变

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

CCTime 用法改变

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

#v2v3
1cc_timevaltimeval
2CCTime::gettimeofdayCocos2dgettimeofday
3CCTime::timesubCocos2dgetTimeDiffenceMS

范例:

:::C++
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);
}

下面的内容为 zrong 原创。


OpenGL 的用法变化

#v2v3
1CCGLProgramGLProgram
3kCCUniformPMatrix_sGLProgram::UNIFORM_NAME_P_MATRIX
4kCCUniformMVMatrix_sGLProgram::UNIFORM_NAME_MV_MATRIX
5kCCUniformMVPMatrix_sGLProgram::UNIFORM_NAME_MVP_MATRIX
6kCCUniformTime_sGLProgram::UNIFORM_NAME_TIME
7kCCUniformSinTime_sGLProgram::UNIFORM_NAME_SIN_TIME
8kCCUniformCosTime_sGLProgram::UNIFORM_NAME_COS_TIME
9kCCUniformRandom01_sGLProgram::UNIFORM_NAME_RANDOM01
10kCCUniformSampler_sGLProgram::UNIFORM_NAME_SAMPLER0
11kCCUniformAlphaTestValueGLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE
12kCCAttributeNameColorGLProgram::ATTRIBUTE_NAME_COLOR
13kCCAttributeNamePositionGLProgram::ATTRIBUTE_NAME_POSITION
14kCCAttributeNameTexCoordGLProgram::ATTRIBUTE_NAME_TEX_COORD

以前我写过一篇 Cocos2d-x中的事件调用方式汇总 ,其中介绍了 cocos2d-x 中的回调函数。而在 v3 版本中,这些回调函数已经完全废弃了。

在 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值