Cocos2D-x Tutorials Part 1: Adding Your Own Layer

In this tutorial I am assuming that you have already installed cocos2d-x in your system if not then follow this link, its not that hard:

create a new project in cocos2d-x

Also we will use C++11 and Cocos2D-x V3 for the tutorials.

Now lets create our layer named TouchMove. In this tutorial we will show how to create our own game scene. Later we will extend it by adding an image and moving it.

Create two files named Touch_and_Move.h and Touch_and_Move.cpp.
Make sure you have moved them in the classes folder.

Now copy and paste the code in the header file:

Now lets look at the class defination before we move to implementation;

Our TouchMove class inherited from the cocod2D Layer class.
At line 5 we see a macro USING_NS_CC which is just a shortcut of using namespace cocos2d;
And at line 14 we see another Macro CREATE_FUNC(TouchMove);
This macro actually creates a function called TouchMove* create()
and inside this method the bool init() method is called and returns a TouchMove* object. Also we dont have to release the object, cocos2d-x release it for us.

There is also a static method called screen() which will give us the size of the device window size. I will explain the Director concept later. Lets just ignore it for now.

Now Lets move to implementation:
Copy-Patse the code in the .cpp file:

In the createScene() method we have createted a scene by calling the Scene::create() method. and the we called the TouchMove::create() methods we got from CREATE_FUNC(TouchMove) macro and it gives us a Layer object which we add with scene by calling scene-> addChild(layer)

We will discuss the Scene , Layer, Director, Sprite…. concepts later. For now lets just try to run the project.

Now go to AppDelegate.cpp file and add the TouchMove header file:
#include "Touch_and_Move.h"

Go to line 30. and modify like this;

auto scene = TouchMove::createScene()

now compile the project and you will see a black window with some digits at the bottom left corner.
We’re done for this tutorial. Next we will add sprite and move them.


Now read this

Cocos2D-x Tutorials Part 4: Actions & Sequence

In Part 3 we were introduced with cocos2d-x actions. In this tutorial we will see in detail how to create actions and run them. Most Actions in cocos2d-x has a format: action_By & action_To like MoveBy & MoveTo. Whats the... Continue →