Code Structure and CCSprite help

Code Structure and CCSprite help
0.0 0


I purchased the book on packtpub for cocos2d-x but i have a few thing that i am still very confused about.

My questions are:

  1. is it correct that i am declaring my CCsprite variables inside the header file? Because for example. If i wanted to display an image in my HelloWorld::init() but then when i receive a touchscreen i want to manipulate that sprite. So the only way to access that sprite is to have it declared in the header file?

  2. A CCSprite is a type of container but does it only contain a single file (png) or can it contain many png files?

  3. If i did this~~>addChild; in my HelloWorld::init function the “this” is referring to the HelloWorld object?

  4. If a CCSprite can contain many images then i guess the tag is the only thing that differentiates them so would a this~~>removeChild(); work?

What i am currently trying to do is to display a sprite and then when it is clicked i want it to animate. but the way i have implemented this makes use of 2 CCSprites and a integer variable that acts as a flag to keep track of what is currently in animation. and i am basically displaying a sprite using the addchild then removing it when it is clicked and displaying the animation and then adding it back after the animation is done. BUT is it possible to do it with just one CCSprite?

Thank you in advance. I’ve read chapter two of the book and i’ve read numerous articles but it still dont seem to understand



Everything in cocos2d is a Node (CCNode). That means virtually everything can have children, and what you do to the parent affects the children in relative coordinates.

A CCSprite is a representation of a texture on the screen. It is a quad with a texture stretched (clamped) on it.

As a node, the CCSprite can have children that are other sprites.

“this” in “this->addChild()” is likely referring to either the CCScene or CCLayer that is your container.

At the root is CCScene. In that scene are nodes. You can layer your nodes by grouping using CCLayer or you can dump all of them into the scene as CCNode instances. The typical pattern is to create a single CCLayer as the root child in a CCScene and then put all of your children into that root CCLayer.


Thanks for your quick reply Jacob, I am starting to understand what you are saying. Am I right to declare my CCSprite variables inside my header file? Because that is the only option I can see that will allow me to move between the “init” function and the “touchesbegan” function.

Also I dont understand what autorelease/retain is and how it differs from new and delete. in what senario will I want to do an autorelease command and a retain?


Yes, declaring CCSprite in header so that you can access it in other functions of the class.

If you want to animate your sprite, use CCAnimate and run the action on sprite. :slight_smile:

About autorelease, release and retain it took some time for me to understand too. :smiley:
This is what i understood.
Consider that your FPS is 60, that means that the scene is being drawn 60 times per second.

The this engine loop happens in two stages:
Stage 1: Draws all the objects on the screen. (Also running all your code including every function you scheduled, ever action you run on CCNode, all your AI calculation etc.)
Stage 2: It checks if there is any object in the pool that has reference count as 0 and delete those objects. Then go to stage 1.

The difference between autorelease and release is:
If you call autorelease on any CCObject, it just decrement reference count by 1. (referenceCount—) But it wont delete the object. It waits till the stage 1 is finished and in stage 2 the object is deleted if its reference count is 0.
But if you call release on any CCObject, it decrements reference count by 1, then checks if reference count is 0 and if it is 0, deletes the object then and there only. It wont wait for stage 1 to finish.

Calling retain will simply increment reference count, there by avoiding the object to be deleted in stage 2. So the object retains in the memory for your future uses.

Now that you know difference between autorelease and release lets go ahead.
When ever you call “create”, it returns you a “autoreleased object”, don’t get baffled by the word it just means object with reference count 0. :wink: Which means unless you do something to increase its reference count, you cant use the object in your next frame, stage 2 will deleted the object from memory. But don’t panic here and call retain, (!) avoid using retain and release as much as you can.

If calling retain is bad thing then how can you make object stay?
Well answer is pretty simple, if you add the node to another node (addChild), its retain count will increase by 1 :smiley: and the moment you remove the node it decrements the reference count (removeChild). So in a way cocos2d-x is taking care of all memory management for you. :stuck_out_tongue:

The only case i encountered, where you have to manually call retain and release is when you create a CCArray or CCPointArray. Since there are not nodes, you cant attach them to any other node. So when you create them you have to call retain on them.

one example explaining reference count:

CCArray *sampleArray = CCArray::create();    // reference count of sampleArray is 0

sampleArray->retain();       // reference count of sampleArray is 1

CCSprite *sampleSprite = CCSPrite::create("sampleImage.png");   // reference count of sampleArray is 1, reference count of sampleSprite is 0

sampleArray->addObject( sampleSprite );   // adding sprite to array, reference count of sampleArray is 1, reference count of sampleSprite is 1

this->addChild( sampleSprite );   // adding sprite to scene, reference count of sampleArray is 1, reference count of sampleSprite is 2

this->removeChild( sampleSprite );   // removing sprite from scene, reference count of sampleArray is 1, reference count of sampleSprite is 1

// even though sampleSprite is removed from scene its reference count is still 1, so it stays in memory. you can access it again using the array

sampleArray->removeObject( sampleSprite );   // removing sprite from array, reference count of sampleArray is 1, reference count of sampleSprite is 0

// at this stage there is no sampleSprite anymore, it is deleted.

Hope this clears it out :slight_smile: