Game Development Reference
If you're deeply concerned by the way Objective-C programmers write the call to
[super init] , here's an alternative that might ease your mind, even though it's not
as common among cocos2d developers. It's fundamentally the same, and Apple also
uses this style:
self = [super init];
if (self != nil)
// do init stuff here . . .
Now let me explain how the label is added to the scene. If you look again at the
ing one of init 's static initializer methods. It'll return a new instance of the CCLa-
belTTF class. To not have its memory freed after control leaves the init meth-
od, you have to add the label to self as a child using the [self ad-
dChild:label] message. In essence, holding a reference to an object keeps the
object alive. In this case, cocos2d takes control over the object's memory until you re-
move it from the scene or switch scenes.
In between creating and adding the label, the label is assigned a position at the center
of the screen. Note that whether you assign the position before or after the call to ad-
dChild doesn't matter.
If you've never done any Objective-C programming before, good for you! You'll come
to take for granted memory management that works as you would expect. As long as
you keep a reference to an object, it will be kept alive. You don't need to worry about
allocating or freeing memory at all.