如何在CCLabelTTF上创建笔划

时间:2013-08-28 16:58:10

标签: cocos2d-iphone stroke cclabelttf strokeshadow

我一直在寻找过去几个小时在cocos2d中围绕标签创建笔划的方法,但到目前为止,我想出的就是:CCLabelTTF Font Stroke Demo这就是我需要的但是中风看起来很块,我需要看起来更顺畅的东西。是否有任何方法可以为中风打开某种抗锯齿,或者可能还有其他方法来创建中风。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

创建笔画:

-(CCRenderTexture*) createStroke: (CCLabelTTF*) label   size:(float)size   color:(ccColor3B)cor
{
    CCRenderTexture* rt = [CCRenderTexture renderTextureWithWidth:label.texture.contentSize.width+size*2  height:label.texture.contentSize.height+size*2];
    CGPoint originalPos = [label position];
    ccColor3B originalColor = [label color];
    BOOL originalVisibility = [label visible];
    [label setColor:cor];
    [label setVisible:YES];
    ccBlendFunc originalBlend = [label blendFunc];
    [label setBlendFunc:(ccBlendFunc) { GL_SRC_ALPHA, GL_ONE }];
    CGPoint bottomLeft = ccp(label.texture.contentSize.width * label.anchorPoint.x + size, label.texture.contentSize.height * label.anchorPoint.y + size);
    //CGPoint positionOffset = ccp(label.texture.contentSize.width * label.anchorPoint.x - label.texture.contentSize.width/2,label.texture.contentSize.height * label.anchorPoint.y - label.texture.contentSize.height/2);
    //use this for adding stoke to its self...
    CGPoint positionOffset= ccp(-label.contentSize.width/2,-label.contentSize.height/2);

    CGPoint position = ccpSub(originalPos, positionOffset);

    [rt begin];
    for (int i=0; i<360; i+=60) // you should optimize that for your needs
    {
        [label setPosition:ccp(bottomLeft.x + sin(CC_DEGREES_TO_RADIANS(i))*size, bottomLeft.y + cos(CC_DEGREES_TO_RADIANS(i))*size)];
        [label visit];
    }
    [rt end];
    [[[rt sprite] texture] setAntiAliasTexParameters];//THIS
    [label setPosition:originalPos];
    [label setColor:originalColor];
    [label setBlendFunc:originalBlend];
    [label setVisible:originalVisibility];
    [rt setPosition:position];
    return rt;
}

用法:

CCRenderTexture* myStroke = [self createStroke:myCCLabelTTF size:myStrokeSize color:ccYELLOW];
[myCCLabelTTF addChild:myStroke z:-1 tag:kTagStroke];

为了增加平滑度,请修改以下功能以满足您的需要(将+60增量减少到+30)。请注意,迭代次数越多,渲染所花费的时间就越多,这将对性能产生负面影响:

for (int i=0; i<360; i+=60) // you should optimize that for your needs
    {
        [label setPosition:ccp(bottomLeft.x + sin(CC_DEGREES_TO_RADIANS(i))*size, bottomLeft.y + cos(CC_DEGREES_TO_RADIANS(i))*size)];
        [label visit];
    }