Implementation of cocos2d-x using cclabelttf to make text stroke and shadow effect

Source: Internet
Author: User
Tags addchild
# Ifndef _ helloworld_scene_h __# DEFINE _ helloworld_scene_h __# include "cocos2d. H "using_ns_cc; using namespace STD; Class helloworld: Public cclayer {public: Virtual bool Init (); static ccscene * scene (); create_func (helloworld ); // Add stroke cclabelttf * textaddstroke (const char * string, const char * fontname, float fontsize, const cccolor3b & color3, float linewidth) to the text ); // Add the shadow cclabelttf * textaddshadow (const char * string, const char * fontname, float fontsize, const cccolor3b & color3, float shadowsize, float shadowopacity ); // Add both the stroke and the shadow cclabelttf * textaddoutlineandshadow (const char * string, const char * fontname, float fontsize, const cccolor3b & color3, float linewidth, float shadowsize, float shadowopacity) ;};# endif // _ helloworld_scene_h __
# Include "helloworldscene. H "# include" simpleaudioengine. H "using namespace cocos2d; using namespace cocosdenshion; ccscene * helloworld: Scene () {ccscene * scene = ccscene: Create (); helloworld * layer = helloworld :: create (); scene-> addchild (layer); Return scene;} bool helloworld: Init () {If (! Cclayer: Init () {return false;} ccsize size = ccdirector: shareddirector ()-> getwinsize (); // create a background cclayercolor * whitelayer = cclayercolor :: create (ccc4 (0,205,205,255), size. width, size. height); this-> addchild (whitelayer); // creates a stroke, such as cclabelttf * outline = textaddstroke ("stroke", "Arial", 40, ccwhite, 1 ); outline-> setposition (CCP (size. width * 0.5, size. height * 0.7); this-> addchild (outline); // creates a shadow. Cclabelttf * shadow = textaddshadow ("shadow", "Arial", 40, ccwhite, 2,200); shadow-> setposition (CCP (size. width * 0.5, size. height * 0.5); this-> addchild (Shadow); // create a stroke and add a shadow, cclabelttf * outlineshadow = textaddoutlineandshadow ("stroke and shadow outlineshadow", "Arial ", 40, ccwhite, 1, 4,200); outlineshadow-> setposition (CCP (size. width * 0.5, size. height * 0.3); this-> addchild (outlineshadow); Return true;}/* create a text description The edge effect is very simple. After writing a piece of text, we create a cclabelttf, which is called the body cclabelttf. Then, four cclabelttf are created. The color is black and the size is the same as that of the body. It is called the stroke cclabelttf. You may have understood this. That's right. Put the four strokes, cclabelttf, under the body of the cclabelttf, on the left and right, and the body of the cclabelttf, respectively, in this way, the stroke effect is achieved .. * String text * fontname text font type * fontsize text size * color3 text color * linewidth stroke width */cclabelttf * helloworld: textaddstroke (const char * string, const char * fontname, float fontsize, const cccolor3b & color3, float linewidth) {// body cclabelttf * center = cclabelttf: Create (string, fontname, fontsize ); center-> setcolor (color3); // stroke cclabelttf * up = cclabelttf: Create (string, fontname, fontsi Ze); Up-> setcolor (ccblack); Up-> setposition (CCP (center-> getcontentsize (). width * 0.5, center-> getcontentsize (). height * 0.5 + linewidth); Center-> addchild (up,-1); // cclabelttf under cclabelttf * down = cclabelttf: Create (string, fontname, fontsize); down-> setcolor (ccblack); down-> setposition (CCP (center-> getcontentsize (). width * 0.5, center-> getcontentsize (). height * 0.5-linewidth); Center-> addchild (down,-1 ); // Stroke cclabelttf left cclabelttf * Left = cclabelttf: Create (string, fontname, fontsize); left-> setposition (CCP (center-> getcontentsize (). width * 0.5-linewidth, center-> getcontentsize (). height * 0.5); left-> setcolor (ccblack); Center-> addchild (left,-1); // stroke cclabelttf right cclabelttf * Right = cclabelttf :: create (string, fontname, fontsize); Right-> setcolor (ccblack); Right-> setposition (CCP (center-> getcontentsize (). Width * 0.5 + linewidth, center-> getcontentsize (). height * 0.5); Center-> addchild (right,-1); Return Center;}/* Add a shadow to the text... * String text * fontname text font type * fontsize text size * color3 text color * shadowsize shadow size * shadowopacity shadow transparency */cclabelttf * helloworld: textaddshadow (const char * string, const char * fontname, float fontsize, const cccolor3b & color3, float shadowsize, float shadowopacity) {cclabelttf * shadow = cclabelttf: Create (string, fontname, fontsize ); shadow-> setcolor (ccblack); shadow-> setopacity (shadowopacity); cclabelttf * center = cclabelttf: Create (string, fontname, fontsize); Center-> setcolor (color3 ); center-> setposition (CCP (shadow-> getcontentsize (). width * 0.5-shadowsize, shadow-> getcontentsize (). height * 0.5 + shadowsize); shadow-> addchild (center); Return shadow;} // Add both the stroke and the shadow, cclabelttf * helloworld :: substring (const char * string, const char * fontname, float fontsize, const cccolor3b & color3, float linewidth, float shadowsize, float shadowopacity) {cclabelttf * center = textstroaddke (string, fontname, fontsize, color3, linewidth); cclabelttf * shadow = cclabelttf: Create (string, fontname, fontsize); shadow-> setposition (CCP (center-> getcontentsize (). width * 0.5 + shadowsize, center-> getcontentsize (). height * 0.5-shadowsize); shadow-> setcolor (ccblack); shadow-> setopacity (shadowopacity); Center-> addchild (shadow,-1); Return Center ;}

Reprinted from http://blog.csdn.net/song_hui_xiang/article/details/17375279

Implementation of cocos2d-x using cclabelttf to make text stroke and shadow effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.