转载于http://www.redtgames.com/blog/cocos2d-x-v2-to-v3-mapping-guide/
cocos2d-x v2 to v3 mapping guide
So I started my next project and decided to use cocos2d-x v3. As soon as I started migrating my common classes,I realized that there are significant changes not only to the naming convention but also usage of some of the classes such as “CCDictionary”,“CCArray”,“CCString” and many more. I will list the transition mapping from v2 to v3 below as I come across. Please feel free to leave a comment if I have missed out anything.cocos2d-x v2 to v3 mapping
The list below maps the transition which has been made in cocos2d-x framework recently. This are minor changes to naming convensions and the underlying logic is the same.
In short the use ofCChas been removed.
# | 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 | CCSpriteBatchNode | SpriteBatchNode |
12 | CCTMXTiledMap | TMXTiledMap |
cocos2d-x v2 to v3 mapping
The changes below are more significant since the underlying usage has been changed as well.
# | v2 | v3 |
---|---|---|
1 | CCDictionary | ValueMap |
2 | CCArray | ValueVector |
3 | CCString | Value |
CCString usage changes
CCDictionary usage changes
Before
After
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 Usage changes
# | v2 | v3 |
---|---|---|
1 | CCArray* sprites | Vector<Sprite *> 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(); |
Below are additional comments provided byFradow
touches Usage changes
# | v2 | v3 |
---|---|---|
1 | ccTouchBegan | onTouchBegan |
2 | ccTouchMoved | onTouchMoved |
3 | ccTouchEnded | onTouchBegan |
EGLView Usage changes
# | v2 | v3 |
---|---|---|
1 | CCEGLView::sharedOpenGLView(); | Director::sharedDirector()->getopenGLView(); |
CCTime Usage changes
# | v2 | v3 |
---|---|---|
1 | cc_timeval | timeval |
2 | CCTime::gettimeofdayCocos2d | gettimeofday |
3 | CCTime::timersubCocos2d | getTimeDifferenceMS |
Sample
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);}
Labels Usage changes
# | v2 | v3 |
---|---|---|
1 | cclabelTTF | Label |
I will keep updating this article as I come across any changes which are not obvIoUs. Please help me in doing so by providing some Feedback and anything I have missed. Thanks for reading.