Game Development Reference
// create and initialize a label
CCLabelTTF* label = [CCLabelTTF labelWithString:@"Hello World"
// get the window (screen) size from CCDirector
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label at the center of the screen
label.position = CGPointMake(size.width / 2, size.height / 2);
// add the label as a child to this Layer
[self addChild: label];
// our label needs a tag so we can find it later on
// you can pick any arbitrary number
label.tag = 13;
// must be enabled if you want to receive touch events!
self.isTouchEnabled = YES;
The label object gets 13 assigned to its tag property. Now why did you do that? I
know, I told you to, but I must have had a reason, right? In the previous section, I ex-
plained that that's how you can later access a child object of your class—you can refer
to it by its tag. The tag number is completely arbitrary, other than that it must be a pos-
itive number and every object should have its own tag number, so there aren't two with
the same number, or you couldn't tell which you'd be retrieving.
Tip Instead of using magic numbers like 13 as tag values, you should get in the
habit of defining constants to use with tags. You'll have a hard time remember-
ing what tag number 13 stands for, compared to writing a meaningful variable
name like kTagForLabel . I get to this in Chapter 5 .
In addition, self.isTouchEnabled is set to YES . This property of the CCLay-
er class tells it that you want to receive touch messages. Only then will the method
ccTouchesBegan be called: