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.

Scene: Like a scene in the film, in cocos2d-x a scene means a game scene. you can think of a scene as a level (Level 1, Level 2…..) for now. Scene can contain one or more Layer.

Node: Node is the base class for Layer, Scene and many cocos2d-x class. Director is not from Node class, Its inherited from Object class. Node is like a container for your app to hold scene, layer,sprite….. data like.

auto background = Sprite::create("background.png");

auto myLayer = Layer::create();

Here background is a node, so is myLayer.

Lets add a Sprite in our previously created TouchMove class:

Header file code:

And cpp file code:

I think adding image was quite easy and no explanation is needed. At line 19 and 23 this-> addChild() method added our sprite in our layer. If you comment out this line, we will not see our image on the screen.

Now lets run the app. If everything is ok then we should see somethig like the image below:

Capture2

In the next tutorial we will move the sprite and see how touch worls in cocos2d-x.

 
85
Kudos
 
85
Kudos

Now read this

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