The game often uses the text stroke and shadow, when cocos2dx does not provide us, the following is my reference: Click to open the Link (http://blog.csdn.net/song_hui_xiang/article/details/17375279) made a, basically the same, don't like it, don't spray it! The Code is as follows:
Idea: Multiple CCLabelTTF overlapping.
Header file
//// CLabel. h // XXX // Created by user on 14-3-4. /// # ifndef _ XXX _ CLabel __# define _ XXX _ CLabel __# include
# Include "cocos2d. h "USING_NS_CC; namespace CLabel {/* Sample 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 = CLabel: textAddOutline ("stroke Outline", "Arial", 40, ccWHITE, 1); outline-> setPosition (ccp (size. width * 0.5, size. height * 0.7); this-> addChild (outline); // create the shadow CCLabelTTF * Shadow = CLabel: 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 = CLabel: 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 ); *///************************************ **************************************// add stroke //********************************* **************************************** * CCLabelTTF * textAddOutline (const char * string, // The displayed text const char * fontName, // font name float fontSize, // font size const ccColor3B & color3, // font color float lineWidth ); // stroke width //********************************* **************************************** * // Add the shadow CCLabelTTF * textAddShadow (const char * string, // The displayed text const char * fontName, // font name float fontSize, // font size const ccColor3B & color3, // font color float shadowSize, // float shadowOpacity ); // shadow transparency //********************************** **************************************** //************************************** * ********************************** // Add add a shadow to the stroke //********************************* **************************************** * CCLabelTTF * textAddOutlineAndShadow (const char * string, // display text const char * fontName, // font name float fontSize, // font size const ccColor3B & color3, // shadow color float lineWidth, // font width float shadowSize, // float shadowOpacity); // shadow transparency}; # endif/* defined (_ XXX _ CLabel __)*/
// Source file
//// CLabel. cpp // XXX // Created by user on 14-3-4. //// # include "CLabel. h "namespace CLabel {/* it is very easy to create a text stroke. 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 * textAddOutline (const char * string, const char * fontName, float fontSize, const ccColor3B & color3, float lineWidth) {// stroke CCLabelTTF left CCLabelTTF * left = CCLabelTTF: create (string, fontName, fontSize ); left-> setColor (ccBLACK); // stroke CCLabelTTF right CCLabelTTF * right = CCLabelTTF: create (string, fontName, fontSize); ri Ght-> setColor (ccBLACK); right-> setPosition (ccp (left-> getContentSize (). width * 0.5 + lineWidth * 2, left-> getContentSize (). height * 0.5); left-> addChild (right); // CCLabelTTF * up = CCLabelTTF: create (string, fontName, fontSize ); up-> setColor (ccBLACK); up-> setPosition (ccp (left-> getContentSize (). width * 0.5 + lineWidth, left-> getContentSize (). height * 0.5 + lineWidth); left-> addChild (up); // stroke CCLabelT Under TF, CCLabelTTF * down = CCLabelTTF: create (string, fontName, fontSize); down-> setColor (ccBLACK); down-> setPosition (ccp (left-> getContentSize (). width * 0.5 + lineWidth, left-> getContentSize (). height * 0.5-lineWidth); left-> addChild (down); // CCLabelTTF * center = CCLabelTTF: create (string, fontName, fontSize ); center-> setColor (color3); center-> setPosition (ccp (left-> getContentSize (). width * 0.5 + l IneWidth, left-> getContentSize (). height * 0.5); left-> addChild (center); return left;}/* Add a shadow to the text. You can understand it at a glance... * String text * fontName text font type * fontSize text size * color3 text color * shadowSize shadow size * shadowOpacity shadow transparency */CCLabelTTF * 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;} // you can add a stroke and a shadow. You can also add the CCLabelTTF * textAddOutlineAndShadow (const char * string, const char * fontName, float fontSize, const ccColor3B & color3, float lineWidth, float shadowSize, float shadowOpacity) {character * shadow = CCLabelTTF: create (string, fontName, fontSize ); shadow-> setColor (ccBLACK); shadow-> setOpacity (shadowOpacity); CCLabelTTF * left = CCLabelTTF: create (string, fontName, fontSize); left-> setColor (ccBLACK ); left-> setPosition (ccp (shadow-> getContentSize (). width * 0.5-shadowSize, shadow-> getContentSize (). height * 0.5 + shadowSize); shadow-> addChild (left); CCLabelTTF * right = CCLabelTTF: create (string, fontName, fontSize); right-> setColor (ccBLACK ); right-> setPosition (ccp (left-> getContentSize (). width * 0.5 + lineWidth * 2, left-> getContentSize (). height * 0.5); left-> addChild (right); CCLabelTTF * up = CCLabelTTF: create (string, fontName, fontSize); up-> setColor (ccBLACK ); up-> setPosition (ccp (left-> getContentSize (). width * 0.5 + lineWidth, left-> getContentSize (). height * 0.5 + lineWidth); left-> addChild (up); CCLabelTTF * down = CCLabelTTF: create (string, fontName, fontSize); down-> setColor (ccBLACK ); down-> setPosition (ccp (left-> getContentSize (). width * 0.5 + lineWidth, left-> getContentSize (). height * 0.5-lineWidth); left-> addChild (down); CCLabelTTF * center = CCLabelTTF: create (string, fontName, fontSize); center-> setColor (color3 ); center-> setPosition (ccp (left-> getContentSize (). width * 0.5 + lineWidth, left-> getContentSize (). height * 0.5); left-> addChild (center); return shadow ;}}
The namespace can be pulled when used. The following is an example:
CCSize size = CCDirector: shareddire()-> getWinSize (); // create a stroke, which is CCLabelTTF * outline = CLabel: textAddOutline ("stroke Outline", "Arial ", 40, ccWHITE, 1); outline-> setPosition (ccp (size. width * 0.5, size. height * 0.7); this-> addChild (outline); // create the shadow CCLabelTTF * Shadow = CLabel: 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 = CLabel: 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 );