How to give shadow effects to label items in cocos2D?
I have moving sprites in my game and have added labels on it (CCLabelTTF). The labels have characters like A, B, C etc. I want to give a shadow effect to the contents of labels so that they are properly visible.
in .h file i have
CCLabelTTF *label;
and in .m i have set its position and color. "target" is the sprite on which there are labels.
target=[CCSprite spriteWithFile:[NSString stringWithFormat:@"balloon%d.png",enType] rect:CGRectMake(0, 0, 100, 119)]; label = [[CCLabelTTF alloc] initWithString:alphabetValue dimensions:CGSizeMake([target contentSize].width, [target contentSize].height) alignment:UITextAlignmentCenter fontName:@"verdana" fontSize:30.0f]; //LABEL POSITION HERE label.position = ccp(55,30); label.color = ccc3(60,60,60); [target addChild:label z: 10];
Now i want to give a shadow effect...how can i do so?
Answers
Add the same label twice, one time slightly bigger. The shadow label should be created first to make it appear behind the actual label, or use the z property.
CGSize size = CGSizeMake([target contentSize].width, [target contentSize].height); labelShadow = [[CCLabelTTF alloc] initWithString:alphabetValue dimensions:size alignment:UITextAlignmentCenter fontName:@"verdana" fontSize:30.0f]; labelShadow.position = ccp(55+2,30+2); // slightly offset labelShadow.color = ccc3(10,10,10); [target addChild:labelShadow z:10]; label = [[CCLabelTTF alloc] initWithString:alphabetValue dimensions:size alignment:UITextAlignmentCenter fontName:@"verdana" fontSize:30.0f]; label.position = ccp(55,30); label.color = ccc3(60,60,60); [target addChild:label z:10];
You can also experiment with slightly scaling the labelShadow up, or increasing its fontSize.
Note: it's not going to create a soft (blurred) shadow. For that you could use the texture filter methods available here.