Skip to content

Commit 7b85ea2

Browse files
committed
Added new comments to CCNode header to explain the "cleanup" mechanism.
Tidied up the CCNode header comment.
1 parent d062c08 commit 7b85ea2

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

cocos2d/CCNode.h

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
@class CCBAnimationManager;
4444
@class CCAnimationManager;
4545

46-
/** CCNode is the base class for all objects displayed by Cocos2D. CCNode handles transformations, can have a content size and provides a coordinate system
47-
for its child nodes.
46+
/**
47+
CCNode is the base class for all objects displayed by Cocos2D. CCNode handles transformations, can have a content size and provides a coordinate system for its child nodes.
4848
4949
### Node Hierarchy
5050
@@ -57,21 +57,18 @@
5757
5858
### Draw Order
5959
60-
Draw order of nodes is controlled primarily by their order in the node hierarchy. The parent node is drawn first, followed by its child nodes in the order they
61-
were added.
60+
Draw order of nodes is controlled primarily by their order in the node hierarchy. The parent node is drawn first, followed by its child nodes in the order they were added.
6261
6362
You can fine-tune draw order via the zOrder property. By default all nodes have a zOrder of 0. Nodes with lower zOrder are drawn before nodes with higher zOrder.
6463
This applies only to nodes in the same level (sibling nodes) and their parent node, as the zOrder is relative to the zOrder of the parent.
6564
66-
Assuming you have two parent nodes A and B with zOrder 0 and they are drawn in the order A first, then B. Then all of the children of parent B will be drawn
67-
in front of any child node of parent A. If B's zOrder is changed to -1, then parent B and all of its children will be drawn behind parent A and its children.
65+
Assuming you have two parent nodes A and B with zOrder 0 and they are drawn in the order A first, then B. Then all of the children of parent B will be drawn in front of any child node of parent A. If B's zOrder is changed to -1, then parent B and all of its children will be drawn behind parent A and its children.
6866
6967
### Scheduling Events / Timers
7068
7169
Implementing a method with a signature of `-(void) update:(CCTime)delta` in a CCNode subclass will have that method run once every frame. `CCTime` is declared as `double`.
7270
73-
If this doesn't suffice you can use the various schedule methods of a node, such as schedule:interval: or scheduleBlock:delay:. For example the following selector runs
74-
once every second:
71+
If this doesn't suffice you can use the various schedule methods of a node, such as schedule:interval: or scheduleBlock:delay:. For example the following selector runs once every second:
7572
7673
[self schedule:@selector(everySecond:) interval:1.0];
7774
@@ -81,57 +78,43 @@
8178
NSLog(@"tic-toc ..");
8279
}
8380
84-
**Warning:** Any non-Cocos2D scheduling methods will be unaffected by the node's paused state and may run in indeterminate order, possibly causing rendering
85-
glitches and timing bugs. It is therfore strongly discouraged to use NSTimer, `performSelector:afterDelay:` or Grand Central Disptach (GCD) `dispatch_xxx` methods
86-
to time/schedule tasks in Cocos2D.
81+
<em>Warning:</em> Any non-Cocos2D scheduling methods will be unaffected by the node's paused state and may run in indeterminate order, possibly causing rendering glitches and timing bugs. It is therfore strongly discouraged to use NSTimer, `performSelector:afterDelay:` or Grand Central Disptach (GCD) `dispatch_xxx` methods to time/schedule tasks in Cocos2D.
8782
8883
#### Pausing
8984
90-
It is common practice to pause the topmost node of a layer whose contents you want to pause. For instance you should have a gameLayer node that you can use
91-
to pause the entire game, while the hudLayer and pauseMenuLayer nodes may not need to or shouldn't be paused in order to continue animations and allowing the
92-
user to interact with a pause menu.
85+
It is common practice to pause the topmost node of a layer whose contents you want to pause. For instance you should have a gameLayer node that you can use to pause the entire game, while the hudLayer and pauseMenuLayer nodes may not need to or shouldn't be paused in order to continue animations and allowing the user to interact with a pause menu.
9386
9487
### Input Handling
9588
9689
Any CCNode or subclass can receive touch and mouse events, if enabled. See the CCResponder super class for more information.
9790
9891
### Position and Size Types
9992
100-
Coordinates in the CCNode coordinate system are by default set in points by the position property. The point measurement provides a way to handle different
101-
screen pixel densities. For instance, on a Retina device one point corresponds to two pixels, but on non-Retina devices point and pixel resolution are identical.
93+
Coordinates in the CCNode coordinate system are by default set in points by the position property. The point measurement provides a way to handle different screen pixel densities. For instance, on a Retina device one point corresponds to two pixels, but on non-Retina devices point and pixel resolution are identical.
10294
103-
By using the positionType property you can specify how a node's position is interpreted. For instance, if you set the type to CCPositionTypeNormalized a
104-
position value of (0.5, 0.5) will place the node in the center of its parent's container. The container is specified by the parent's contentSize.
95+
By using the positionType property you can specify how a node's position is interpreted. For instance, if you set the type to CCPositionTypeNormalized a position value of (0.5, 0.5) will place the node in the center of its parent's container. The container is specified by the parent's contentSize.
10596
10697
It's also possible to set positions relative to the different corners of the parent's container. The CCPositionType has three components, xUnit, yUnit and corner.
10798
The corner can be any reference corner of the parent's container and the xUnit and yUnit can be any of the following:
10899
109100
- CCPositionUnitPoints - This is the default, the position value will be in points.
110101
- CCPositionUnitScaled - The position is scaled by the UIScaleFactor as defined by CCDirector. This is very useful for scaling up game play without changing the game logic.
111102
E.g. if you want to support both phones and tablets in native resolutions.
112-
- CCPositionUnitNormalized - Using the normalized type allows you to position object in relative to the parents container. E.g. it can be used to center nodes
113-
on the screen regardless of the device type your game is running on.
103+
- CCPositionUnitNormalized - Using the normalized type allows you to position object in relative to the parents container. E.g. it can be used to center nodes on the screen regardless of the device type your game is running on.
114104
115-
Similarily to how you set a node's position and positionType you can also set it's contentSize and contentSizeType. However, some classes doesn't allow you
116-
to set these directly. For instance, the CCSprite sets its contentSize depending on the size of its texture and for descendants of CCControl you should
117-
set the preferredSize and preferredSizeType rather than changing their contentSize directly. The CCSizeType has two components widthUnit and heightUnit
118-
which can be any of the following:
105+
Similarily to how you set a node's position and positionType you can also set it's contentSize and contentSizeType. However, some classes doesn't allow you to set these directly. For instance, the CCSprite sets its contentSize depending on the size of its texture and for descendants of CCControl you should set the preferredSize and preferredSizeType rather than changing their contentSize directly. The CCSizeType has two components widthUnit and heightUnit which can be any of the following:
119106
120107
- CCSizeUnitPoints - This is the default, the size will be in points
121108
- CCSizeUnitScaled - The size is scaled by the UIScaleFactor.
122109
- CCSizeUnitNormalized - The content size will be set as a normalized value of the parent's container.
123110
- CCSizeUnitInset - The content size will be the size of it's parent container, but inset by a number of points.
124111
- CCSizeUnitInsetScaled - The content size will be the size of it's parent container, but inset by a number of points multiplied by the UIScaleFactor.
125112
126-
Even if the positions and content sizes are not set in points you can use actions to animate the nodes. See the examples and tests for more information on
127-
how to set positions and content sizes, or use SpriteBuilder to easily play around with the settings. There are also more positioning options available
128-
by using CCLayout and CCLayoutBox.
113+
Even if the positions and content sizes are not set in points you can use actions to animate the nodes. See the examples and tests for more information on how to set positions and content sizes, or use SpriteBuilder to easily play around with the settings. There are also more positioning options available by using CCLayout and CCLayoutBox.
129114
130115
#### Prefer to use ..InPoints
131116
132-
There are typically two properties of each property supporting a "type". For instance the position property returns the raw values whose meaning
133-
depends on positionType, while positionInPoints will return the position in points regardless of positionType. It is recommended to use the "inPoints"
134-
variants of properties if you expect the values to be in points.
117+
There are typically two properties of each property supporting a "type". For instance the position property returns the raw values whose meaning depends on positionType, while positionInPoints will return the position in points regardless of positionType. It is recommended to use the "inPoints" variants of properties if you expect the values to be in points.
135118
136119
Otherwise your code will break if you subsequently change the positionType to something other than points (ie UIPoints or Normalized).
137120
@@ -143,14 +126,30 @@
143126
144127
A separate model could simply be any NSObject class initialized by the node subclass and assigned to an ivar/property.
145128
146-
An advanced subclassing style aims to minimize subclassing node classes except for CCNode itself. A CCNode subclass acts as the controller for its node tree,
147-
with one or more child nodes representing the controller node's views. This is particularly useful for composite nodes, such as a player
148-
with multiple body parts (head, torso, limbs), attachments (armor, weapons) and effects (health bar, name label, selection rectangle, particle effects).
129+
An advanced subclassing style aims to minimize subclassing node classes except for CCNode itself. A CCNode subclass acts as the controller for its node tree, with one or more child nodes representing the controller node's views. This is particularly useful for composite nodes, such as a player with multiple body parts (head, torso, limbs), attachments (armor, weapons) and effects (health bar, name label, selection rectangle, particle effects).
149130
150-
The userObject property can be used to add custom data and methods (model, components) to any node, in particular to avoid subclassing where the subclass
151-
would only add minimal functionality or just data.
152-
*/
131+
The userObject property can be used to add custom data and methods (model, components) to any node, in particular to avoid subclassing where the subclass would only add minimal functionality or just data.
132+
133+
### Cleanup of nodes
134+
135+
When a node is no longer needed, and is removed (directly or indirectly) from it's parent by one of:
136+
137+
-(void) removeFromParentAndCleanup:(BOOL)cleanup;
138+
-(void) removeChild: (CCNode*)node cleanup:(BOOL)cleanup;
139+
-(void) removeChildByName:(NSString*)name cleanup:(BOOL)cleanup;
140+
-(void) removeAllChildrenWithCleanup:(BOOL)cleanup;
153141
142+
and the cleanup parameter is YES, the private method:
143+
144+
- (void) cleanup;
145+
146+
is called. This offers an opportunity for the node to carry out any cleanup such as removing possible circular references that might cause a memory leak.
147+
148+
CCNode implements a base level of functionality for cleanup and if your subclass needs to implement it, you will need to import CCNode_Private.h.
149+
150+
@note that if you override cleanup, you must call [super cleanup] <em>after</em> any cleanup of your own.
151+
152+
*/
154153
@interface CCNode : CCResponder < CCSchedulerTarget > {
155154

156155
// Rotation angle.

0 commit comments

Comments
 (0)