Very Basic cocos 2d questions
I'm new to cocos2d so I am a b开发者_如何学Goit confused by some things:
The term "child" keeps popping up. At first I thought it was a subclass, now I perceive to be a sort of dynamic instance variable? Can anyone explain this a bit better?
The class
CCSpriteBatchNode
is some kind of array ofCCSprites
which calls a method that renders the graphical part of its elements/children from a sprite sheet. Is my explanation somewhat correct?My book (learning cocos 2d a beginners guide) shows a design setup where the
CCSpriteBatchNode
has several objects as children. I'm a bit confused because I'm used to separating drawing classes from logical classes, AKA "Separation of concerns". Is this something cocos2d doesn't abide by and we shouldn't either?
Parent-child is just a relationship between CCNode and their subclasses in cocos2d. For example if you want a moon turning around the moving planet it is really hard to calculate the absolute path of the moon. But you can make it the child of a planet, and in this case its position, rotation and so on, will be relative to the planet's coordinates. So parent-child relationship is just grouping.
CCSpriteBatchNode is a parent to all its children, but the position of its children are relative to CCSpriteBatchNode parent. Such approach is used because BatchNode is rendering all its children at the same time. That is done to increase performance (no texture switching).
Batch rendering is faster. If your logic is good, CCSpriteBatchNode does not make it worse. For example, you can have a character with a body, arms and legs and you want to render it using BatchNode. You just add the body as a child to BatchNode, then add arms and legs as children to the body. Logic is kept, performance is increased.
精彩评论