【问题标题】:How to create a stroke on a CCLabelTTF如何在 CCLabelTTF 上创建笔画
【发布时间】:2013-08-28 16:58:09
【问题描述】:

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

【问题讨论】:

    标签: cocos2d-iphone stroke cclabelttf strokeshadow


    【解决方案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];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 2011-10-26
      • 2022-11-20
      • 2010-10-31
      • 1970-01-01
      相关资源
      最近更新 更多