Cocos2dx Text Outline 



Here is sample code for text runtime creation with outline attribute.

Code implementation is simple, just check below code. 

enableOutline attribute has input as Color4B with outline size. so you can set any color in Color4B. for example, if you want to set red color, just use pre-define Color4B::RED or put RGB color directly Color4B(255,0,0,255). forth value is Alpha code which is opacity. Important thing is that you must set outline size, if you forgot to set it, default value is 1. 



auto msg = Text::create();

msg->setFontName("font/kenpixel.ttf"); 

msg->setFontSize(30);                       

msg->enableOutline(Color4B::RED, 3);   

msg->setString("Font creation");   

msg->setPosition(Vec2(100, 100);         

this->addChild(msg);                         



That's it. very simple.


Here is addition code what I used text outline effect for new game app. This sample code is that show text when player get item with different type of color. 



void GameScene::showText(const int &iType, const Vec2 &iAt, const std::string &iMsg)

{

auto msg = Text::create();

msg->setFontName("font/kenpixel.ttf");

msg->setFontSize(30);

if(iType == ITEM_HP)

msg->enableOutline(Color4B::RED, 3); 

else if(iType == ITEM_THROW)

msg->enableOutline(Color4B(80, 51,29, 255), 3);

else if(iType == ITEM_SCORE)

msg->enableOutline(Color4B(150, 100, 60, 255), 3);

else if(iType == ITEM_KEY)

msg->enableOutline(Color4B(230, 196, 0, 255), 3);


msg->setString(iMsg);

msg->setPosition(iAt);

msg->runAction(Sequence::create(FadeIn::create(0.1f), MoveBy::create(0.3f, Vec2(0, 30)), FadeOut::create(0.1f), RemoveSelf::create(), NULL));

_pTileMap->addChild(msg);

}




Check video, implemented code will working like this.





Cocos2D 게임 프로그래밍
국내도서
저자 : 로드 스트로우고(Rod Strougo),레이 웬더리히 / 정기훈역
출판 : 정보문화사 2015.01.09
상세보기