文章撰寫日期︰2013/08/14 14:01 cocos2dx使用版本︰v 2.0.4
cocosBuilder使用版本︰v 2.0 alpha 1
一、前言
想要將CCSpirte或CCLabelTTF增加邊框,該怎麼做?
二、本文開始
爬文至cocos2dx討論版後,發現網友Alexandr Gerasimov熱心地將另一位網友cocos2d的code改成c++語法,
供我們使用。
但是我在使用時,
發現物件一旦被設定過AnchorPoint,
這個繪製岀來的邊框就會跑掉。
因此加了AnchorPoint的判斷,
更改的源碼如下︰
static CCRenderTexture* createStroke(CCSprite* label, int size, ccColor3B color, GLubyte opacity) { CCRenderTexture* rt = CCRenderTexture::create(label->getTexture()->getContentSize().width + size * 2, label->getTexture()->getContentSize().height+size * 2); CCPoint originalPos = label->getPosition(); ccColor3B originalColor = label->getColor(); GLubyte originalOpacity = label->getOpacity(); label->setColor(color); label->setOpacity(opacity); bool originalVisibility = label->isVisible(); ccBlendFunc originalBlend = label->getBlendFunc(); ccBlendFunc bf = {GL_SRC_ALPHA, GL_ONE}; label->setBlendFunc(bf); CCPoint bottomLeft = ccp( label->getTexture()->getContentSize().width * label->getAnchorPoint().x + size, label->getTexture()->getContentSize().height * label->getAnchorPoint().y + size); //原來沒有判斷AnchorPoint的寫法 // CCPoint positionOffset= ccp( - label->getTexture()->getContentSize().width / 2, // - label->getTexture()->getContentSize().height / 2); //SetAnchorPoint會影響到positionOffset,所以要做判斷 CCPoint positionOffset = CCPointZero; if(label->getAnchorPoint().x == 0.5f){ positionOffset.x = 0; }else if(label->getAnchorPoint().x == 0.0f){ positionOffset.x =- label->getTexture()->getContentSize().width / 2; }else{ positionOffset.x = label->getTexture()->getContentSize().width/2; } if(label->getAnchorPoint().y == 0.5f){ positionOffset.y = 0; }else if(label->getAnchorPoint().y == 0.0f){ positionOffset.y =- label->getTexture()->getContentSize().height / 2; }else{ positionOffset.y = label->getTexture()->getContentSize().height/2; } CCPoint position = ccpSub(originalPos, positionOffset); rt->begin(); for (int i=0; i<360 data-blogger-escaped-for="" data-blogger-escaped-i="" data-blogger-escaped-label-="" data-blogger-escaped-needs="" data-blogger-escaped-optimize="" data-blogger-escaped-should="" data-blogger-escaped-that="" data-blogger-escaped-you="" data-blogger-escaped-your="">setPosition( ccp(bottomLeft.x + sin(CC_DEGREES_TO_RADIANS(i))*size, bottomLeft.y + cos(CC_DEGREES_TO_RADIANS(i))*size) ); label->visit(); } rt->end(); label->setPosition(originalPos); label->setColor(originalColor); label->setBlendFunc(originalBlend); label->setVisible(originalVisibility); label->setOpacity(originalOpacity); rt->setPosition(position); //反踞齒 rt->getSprite()->getTexture()->setAntiAliasTexParameters(); return rt; }
結果如下
沒有留言 :
張貼留言