2013-08-28 37 views
1

我一直在尋找過去幾個小時的方法來在cocos2d中創建一個圍繞標籤的筆劃,但到目前爲止我所有的想法都是這樣的:CCLabelTTF Font Stroke Demo這就是我需要,但中風看起來非常塊,我需要看起來更順暢的東西。有什麼方法可以打開某種中風的抗鋸齒功能,或者有其他方法來創建中風。任何幫助,將不勝感激。如何在CCLabelTTF上創建筆畫

回答

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]; 
    }