farhan tanvir

Read this first

Cocos2D-x Tutorials Part 6: Switching between scenes

In this tutorial we are going to see how to create a menu for our game that will let us switch between different game scenes.

There are multiple ways to design a menu in cocos2d-x. In the default generated code we can see how to create a menu with sprites. Here we will see how we can create our menu with LabelTTF or simple text. The process is quite simple. Lets see some code:





Go to AppDelegate.cpp and change the Line 33 :

auto scene = MenuScene::createScene();

Also include these 2 lines after Line 2:

include "MenuScene.h"

include "Scene1.h"

We have all the code in our hand, so lets see what the code is doing for us:

First of all, let us write the steps to create a menu:

  1. Create a LabelTTF object. (MenuScene.cpp line 31 and 32)
  2. Create a MenuItemLabel with the label object with a callback attached. (Line...

Continue reading →

Cocos2D-x Tutorials Part 5: Schedule update and Actions Pause and Resume

In the previous tutorial we saw how to create actions and run them in single order or sequentially. In this tutorial we will see how to schedule a function that will be called on every frame and how can we pause and resume the whole scene or specific schedule/action.

Before we proceed lets learn about one important Cocos2D-x function and that is CCLOG.
CCLOG can print your data in console. Its like printf()/ function in C.

if you don’t know about .VA_ARGS. this is the right time to learn about it and see its real implementation, but that’s not needed for now.

CCLOG("window width is %.1f and height is %.1f", Director::getInstance()->getWinSize().width, Director::getInstance()->getWinSize().height);

Now what is a scheduler ?? A scheduler is a function that will keep executing until we want him to stop. We can also resume it if stopped.

For example we have a cloud sprite and...

Continue reading →

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 difference between them ? well it’s simple, action_To executes only once and then stops where action_By executes the action as many times as you want.

Now lets learn to create an action.

auto jump = JumpTo::create(1.f, Point(0,0), 150, 4);

Here the first parameter is the time the action will take to complete.

The 2nd parameter take a Point where the sprite will land after completing the jump.

Third parameter takes the jump height.

Fourth parameter takes the number of jump.

Lets run the action:

ball-> runAction(jump);

Lets see the entire code:



You can create other actions similarly.

But if you want to...

Continue reading →

Cocos2D-x Tutorials Part 3: Moving a Sprite

In Part 1 and Part 2 we learned to add our layer and sprite.
In this tutorial we will move our sprite where we touched on the screen. Or more generally speaking we will see how to apply an Action on a sprite. Also we will take a look at the touch event.

So lets begin with cocos2d-x Action, for this tutorial the action will be MoveTo.
What does an Action mean ??? well, its simple, it means to do something with a sprite or in a broader sense do something with a node (Sprite is a cocos2d-x Node object). You can Rotate a sprite, you can move it, you can jump it, skew it …… and what not !!!

In this tutorial we will do something like this: we will touch somewhere on our screen and our sprite will move to that particular location.

Lets add the code at our cpp file. we dont need any change in our header file from the last tutorial.

cocos2d-x has 3 types of Touch Events :

  1. on Touch...

Continue reading →

Cocos2D-x Tutorials Part 2: Adding Sprite

In the previous tutorial we saw how to create our own layer.
Tutorial 1

Now its time to start doing something cool…So lets add some sprites and do some actions on them.

But before that we need to go through some concept.

Director: Every cocos2d-x app has a Director who actually runs the app. In a film the director decides which scene to shoot, when to cut the scene and move on for the next shoot and when to pack-up !!!. And normally every film has one Director. Like in real life every cocos2d-x app has a Dierector who can start a scene, move to the next scene, pause the app……etc.

Director::getInstance()-> runWithScene(GameScene)

Director is a Singleton object. If you dont know what singleton is, then please pause here and google it. It basically means that a class can have only one instance and if you try to create more instance you will always get that one and only instance.


Continue reading →

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...

Continue reading →