Game Development Reference
In-Depth Information
-(void) ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
CCLabelTTF* label = (CCLabelTTF*)[self getChildByTag:13];
label.scale = CCRANDOM_0_1();
By using [self getChildByTag:13] , you can access the CCLabelTTF ob-
ject by its tag property, which you assigned in the init method. You can then
use the label as usual. In this case, we use cocos2d's handy
CCRANDOM_0_1() macro to change the label's scale property to a value
between 0 and 1. This will change the label's size every time you touch the screen.
Because getChildByTag will always return the label, you can safely cast it to
a (CCLabelTTF*) object. However, be aware that doing so will crash your game if
the retrieved object isn't derived from the CCLabelTTF class for some reason. This
could easily happen if you accidentally give another object the same tag number 13.
For that reason, it's good practice to use a defensive programming style and verify that
what you're working with is exactly what you expect. Defensive programming uses as-
sertions to verify that assumptions made are true. For this, you should use the
NSAssert method:
-(void) ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
CCNode* node = [self getChildByTag:13];
// defensive programming: verify the returned node is a CCLabelTTF
NSAssert([node isKindOfClass:[CCLabelTTF class]],
@"node is not a CCLabelTTF!");
CCLabelTTF* label = (CCLabelTTF*)node;
label.scale = CCRANDOM_0_1();
Search Nedrilad ::

Custom Search