如何给cocos2D中的标签项目添加阴影效果?
问题描述:
我在我的游戏中移动了精灵,并在其上添加了标签(CCLabelTTF)。标签有A,B,C等字符。我想对标签的内容给予阴影效果,以便它们可以正常显示。如何给cocos2D中的标签项目添加阴影效果?
in .h file我有
CCLabelTTF * label;
在.m中我已设置其位置和颜色。 “目标”是有标签的精灵。
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];
现在我想给一个阴影效果......我该怎么办呢?
答
添加相同的标签两次,一次稍大。应首先创建阴影标签,使其出现在实际标签的后面,或使用z属性。
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];
您还可以尝试稍微缩放labelShadow,或增加其fontSize。
注意:它不会创建柔和(模糊)的阴影。为此,您可以使用texture filter methods available here。