CCNode
Sometimes called a scene hierarchy, the scene graph is a hierarchy of every Cocos2d node that's currently active.
We call them nodes because they inherit from the class CCNode
(http://www.cocos2d-swift.org/docs/api/Classes/CCNode.html), the base class to display objects in Cocos2d.
The usual picture of a scene graph is a root CCScene
node with several node children such as CCButton
, CCSprite
, and CCLabelTTF
. For example, the graph of an initial Cocos2d project looks like the following:
One important feature of CCNode
in the current Cocos2d version is that it inherits from CCResponder
, which allows all the objects derived from CCNode
to handle user touches and mouse events.
I said that CCNode
is the base class to display objects, but what are the classes that inherit from it? You can see the CCNode
class hierarchy in the following graph:
As I mentioned in the previous section, scenes will no longer derive from CCLayer
(as has been the case in previous Cocos2d versions) and if you look closely at the CCNode
class hierarchy in the preceding graph, you will realize that this class doesn't appear in it. It means that CCLayer
has been deprecated and doesn't exist anymore, due to the fact that CCNode
now supports user interaction, which was basically the purpose of CCLayer
besides grouping nodes, a task perfectly accomplished by CCScene
.
One important CCNode
property is positionType
. This property makes reference to how a node's position will be interpreted and can take three different values:
CCPositionUnitPoints
: The node's position will be set in points. This is the default value.CCPositionUnitScaled
: The node's position will be scaled, for example, to be supported by iPhone and iPad.CCPositionUnitNormalized
: The node's position will be relative to its parent node.
Another important property is contentSizeType
, which means that the size of the node's content will be interpreted in different ways depending on its value:
CCSizeUnitPoints
: This is the default value and means that the size will be set in pointsCCSizeUnitScaled
: The node's content size will be scaled by the factor indicatedCCSizeUnitNormalized
: The size will be relative to its parent container sizeCCSizeUnitInset
: The size of the node's content will be the same as its parent node, but it will be inset by the indicated valueCCSizeUnitInsetScaled
: The size of the node's content will be the same as its parent node, but it will be inset by the indicated value that will be multiplied by a scale factor
This class is one of the most important because our games will have plenty of classes that derive from it, such as CCButton
(inherits from CCControl
), CCParallaxNode
, CCParticleSystemBase
, CCSprite
, CCLabelTTF
(inherits from CCSprite
), CCLabelBMFont
(inherits from CCSpriteBatchNode
), or CCScene
.