Developers Manual > Cocos2d-x > How To > User Contributed Tutorials > User Tutorial-CPP Migration from v2 2 * to v3 0

C++ Migration from v2 2 * to v3 0

Contributed By: buntynara

I hope this tutorial will help you guys.
This is tested in the cocos2d-x v3.0.
After installation for v3.0, following the steps will help you to migrate the v2.2 version game to v3.0 version.

1.create the new project by executing following command on terminal

cocos new game -p com.yourcompany.game -l cpp -d destination_path

You should get the following output on the terminal

Runing command: new
Copy template into destination_path
Copying cocos2d-x files...
Rename project name from 'HelloCpp' to 'game'
Replace the project name from 'HelloCpp' to 'game'
Replace the project package name from 'org.cocos2dx.hellocpp' to 'com.yourcompany.game'

2. Replace application icons, defaults, info plist and prefix

Go to game folder at destination_path.

For IOS, Replace the application icons and default images in the proj.ios_mac/ios/ folder with required ones.
You can remove the icons and defaults that are not used in the previous v2.2.* game.
At last replace the info.plist and prefix.pch file with the one in previous v2.2.* game.

For MAC, Replace Icon.icns in the proj.ios_mac/mac/ with required one.
For Android, replace icons in the proj.android/res/drawable-*/icon.png.

3. Open the game.xcodeproj and fix the errors

If you explore the project, you may see the red colored icons in the iOS folder you have removed the un-necessary icons in 2nd step.
Remove that references by clicking on those red colored icons and then delete.

Remove the existing class files in the classes folder and existing images in the Resources folder.
Import the classes including AppDelegate class and resources including images, fonts, plist, sounds, etc. from v2.2 game with copy option marked.
After importing all, there is on small change the code CCEGLView::sharedOpenGLView(); to pDirector->getOpenGLView(); in AppDelegate.cpp file.

Thus finally we have the project with v3.0 version and all assets in it.

4. Fixing the errors that raises due to missing functions

This is our second phase where it starts debugging and replacing the code.
If you have used the setFlippedY or setFlippedX methods on labelTTF in the v2.2.* version game, you will see the error related with it.

Goto CCLabel.h and paste under virtual void setBlendFunc(const BlendFunc &blendFunc) override; declaration

1
2
    virtual void setFlippedX(bool flippedX);
    virtual void setFlippedY(bool flippedY);

Goto CCLabel.cpp and paste under void Label::setBlendFunc(const BlendFunc &blendFunc) function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    void Label::setFlippedX(bool flippedX)
    {
        if (flippedX)
        {
            this->setScaleX(-1.0f);
        }
        else
        {
            this->setScaleX(1.0f);
        }
    }

    void Label::setFlippedY(bool flippedY)
    {
        if (flippedY)
        {
            this->setScaleY(-1.0f);
        }
        else
        {
            this->setScaleY(1.0f);
        }
    }

5. Fixing the errors that raises due to touch handling function

Create the listener based on your requirement

1
2
    EventListenerTouchAllAtOnce* listener1;
    EventListenerTouchOneByOne* listener2;

Change prefix from ‘cc’ to ‘on’ in both .h and .cpp files

1
2
3
    virtual bool onTouchBegan(CCTouch* touch, CCEvent* event);
    virtual void onTouchMoved(CCTouch* touch, CCEvent* event);
    virtual void onTouchEnded(CCTouch* touch, CCEvent* event);

Add the following lines in the class constructor, to init listener and assigning values

1
2
3
4
5
6
7
8
9
10
11
12
    Constructor() 
    {
        listener = EventListenerTouchOneByOne::create();
        listener->setSwallowTouches(true);
        listener->onTouchBegan = CC_CALLBACK_2(GamePlay::onTouchBegan, this);
        listener->onTouchMoved = CC_CALLBACK_2(GamePlay::onTouchMoved, this);
        listener->onTouchEnded = CC_CALLBACK_2(GamePlay::onTouchEnded, this);
        listener->retain();

        this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
        listener->setEnabled(false); // This method is used to enable/disable the touch whenever required
    }

Add the following lines in the class destructor, to remove touch handling and release memory

1
2
3
4
5
6
    ~Destructor() 
    {
        this->getEventDispatcher()->removeEventListener(listener);
        listener->release();
        listener = nullptr;
    }

The CCNS help class has change the function name
Change CCPointFromString to PointFromString

6. Fixing the errors that raises due to CCArray or CCDictionary

There will be warnings due to CCArray or CCDictionary, but it is ok for now.
If you get actual errors due to CCArray or CCDictionary, then it is required to follow this steps, otherwise you can skip.

CCArray changes

Change Array* frames; => Vector<SpriteFrame *> frames;
Change frames->addObject(frame); => frames.pushBack(frame);
Change frames->removeObject(frame); => frames.eraseObject(frame);
Change frames->removeObjectAtIndex(i); => frames.erase(i);
Change frames->objectAtIndex(i); => frames.at(i);
Change frames->->count(); => frames.capacity();

This way in the example above, the Vector is used in replace of Array.

CCDictionary changes

Change CCDictionary* data; => ValueMap data;

Change data = CCDictionary::createWithContentsOfFile(“name.plist");
TO
std::string path = FileUtils::getInstance()->fullPathForFilename(“name.plist");
data = FileUtils::getInstance()->getValueMapFromFile(path);

Change CCArray* arrLevels = (CCArray*) dctData->objectForKey("Levels");
TO
auto arrLevels = dctData.at("Levels").asValueVector();

The casting function as<type> you can use on ValueMap and Vector template type e.g ‘asInt’ is also valid for integer casting
This way in the example above, the ValueMap is used in replace of CCDictioanry.

7. Android: Fixing the errors that raises due to Back-key handling

Change keyBackClicked to onKeyReleased

This way you will be able to remove all the errors able to run the game at least.
Next steps would help to remove the CC prefix from classes and some constants.
It is not necessary, but by following these steps the game will be fully compatible with v3.0 and in future you will face less errors.
Before following the below steps, please take backup because it may happen you mess-up with errors and something may went wrong.

8. Remove the use of CCString class and use std::string which is easier manipulate strings

Change CCString* => std::string
Remove ->getCString()
Change ->compare to .compare
Change ->length to .length (there might be other functions, replace same way)
Change new String("User") to "User”
Remove delete strName;

This way there should be no use of CCString, other wise you will get un-expected errors after executing below command.
So make sure there is no use of CCString in any of classes.

9. Execute the following commands to search and replace the things very quickly from terminal

Please be careful while firing the commands, it is compatible on bash OSX.
It may not work on Windows OS, so you can do search and replace manually in the IDA (xCode or Eclipse)
Go to your classes folder, where your h and cpp files exists, and fire these commands one by one.
For avoiding errors, make sure you close the IDE, xCode or Eclipse.
This is not the full list of search and replace command, there may be possible for other things.

Before removing the prefix CC

find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/CCObject/Ref/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/kCCProgressTimerTypeBar/ProgressTimer::Type::BAR/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/kCCVerticalTextAlignmentCenter/TextVAlignment::CENTER/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/kCCTextAlignmentCenter/TextHAlignment::CENTER/g" '{}' \;

Remove the prefix CC

find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/CC//g" '{}' \;

Add CC back that is removed un-expectedly executing above command

find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/_TARGET_PLATFORM/CC_TARGET_PLATFORM/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/_PLATFORM_/CC_PLATFORM_/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/LOG(/CCLOG(/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/kTouchesOneByOne/kCCTouchesOneByOne/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/USING_NS_/USING_NS_CC/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/_DEGREES_TO_RADIANS/CC_DEGREES_TO_RADIANS/g" '{}' \;

Others

find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/shared[a-zA-z]*()/getInstance()/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/SizeMake/Size/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/ccp(/Point(/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/ccc3(/Color3B(/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/ccc4(/Color4B(/g" '{}' \;

find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/PointZero/Point::ZERO/g" '{}' \;

find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/ccColor3B/Color3B/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/ccBlack/Color3B::BLACK/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/ccWHITE/Color3B::WHITE/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/ccGRAY/Color3B::GRAY/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/ccRED/Color3B::RED/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/ccGREEN/Color3B::GREEN/g" '{}' \;
find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/ccBLUE/Color3B::BLUE/g" '{}' \;

find . ( -iname *.h -o -iname *.cpp ) -exec sed -i -e "s/spriteFrameByName/getSpriteFrameByName/g" '{}' \;

10. Fixing the errors that raises due to Label handling

Change LabelTTF to Label
Change Label::create to Label::createWithSystemFont

11. Fixing the other errors by using IDE

In this final step, you will open the IDE or xCode again and try to build the game as well as fix the errors that are yet missed in this tutorial.

Thanks

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.