Halley Yu wrote:
the reason for adding a SBN as a child,in my opinion, is to retain the SBN.
If you don’t do this, the SBN will be released after the current running loop.
You’re mixing 2 different things here. The parenting system and the memory management.
I’ll first explain the parenting system (as it’s part of the original question):
Cocos2d-x usually handles all the drawing for you. The way the framework knows what to draw is because you’ve told it.
So how do you tell the framework to draw something? Using parenting. (addChild methods)
Assuming you are already drawing a scene (you’ve called runWithScene, replaceScene or pushScene on the director), you add things to this scene’s drawing calling the addChild method on it.
// in TestScene.h
class TestScene : public cocos2d::CCScene
virtual bool init();
// in TestScene.cpp
using namespace cocos2d;
CCNode* sprite = CCSprite::create("someTexture.png"); // addChild accepts any CCNode subclass
this->addChild(sprite); // this is adding the sprite to the collection of nodes it has to draw
CCSpriteBatchNode is a subclass of CCNode, therefor, you can add it to another node with the addChild method and you can also addChilds to it.
If you only add a CCSpriteBatchNode to an scene you won’t be drawing anything, as you still need to add sprites to the SpriteBatch.
Execution goes this way:
* CCDirector calls drawing code on your scene
* Your scene calls drawing code on all it’s children (the CCSpriteBatch node)
* The CCSpriteBatchNode calls drawing code on it’s children (sprites).
* Sprites draw themselves.
If you remove the addChild for the spriteBatch you won’t be drawing it’s children.
When you call parent->addChild(child), parent retains (so it doesn’t get deleted) the child. When the parent is deleted it removes all of it’s children, then they get released.