Cocos2d-x the method of making text stroke and shadow effect by using Cclabelttf

Source: Internet
Author: User
Tags addchild bool

Thanks for the comments and attention, welcome to reprint and share.
Hard work and perseverance.

Method One:

Cocos2d-x has been packaged for us, and the method is in class Cclabelttf.

/** enable or disable shadow for the label *
    /void Enableshadow (const ccsize &shadowoffset, float shadowopacity, F Loat Shadowblur, bool mustupdatetexture = true);
    
    /** Disable Shadow Rendering *
    /void Disableshadow (bool mustupdatetexture = true);
    
    /** Enable or disable stroke *
    /void Enablestroke (const cccolor3b &strokecolor, float strokesize, bool Mustupdatet Exture = true);
    
    /** Disable stroke *
    /void Disablestroke (bool mustupdatetexture = TRUE);


method Two: self-realization

In the case of the method can not reach the effect of the situation, only to achieve their own.

. h file

#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 a stroke to text
    cclabelttf* textaddstroke (const char* string, const char* fontname, float fontsize,const cccolor3b & Color3,float linewidth);
    
    Add Shadow
    cclabelttf* Textaddshadow (const char* string, const char* fontname, float fontsize,const cccolor3b & Color3,float shadowsize,float shadowopacity);
    
    Add strokes and add Shadows
    cclabelttf* Textaddoutlineandshadow (const char* string, const char* FontName, float fontsize,const cccolor3b &color3,float linewidth,float shadowsize,float shadowopacity);
    
};

#endif//__helloworld_scene_h__


. cpp Files

#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);
    Create stroke cclabelttf* outline = textaddstroke ("Stroke Stroke", "Arial", Max, Ccwhite, 1);
    Outline->setposition (CCP (size.width*0.5, size.height*0.7));
    
    This->addchild (outline);
    Create Shadow cclabelttf* shadow = Textaddshadow ("Shadow Shadow", "Arial", Max, Ccwhite, 2, 200);
    Shadow->setposition (CCP (size.width*0.5, size.height*0.5));
    
    This->addchild (shadow); //Create stroke and shadow cclabelttf* Outlineshadow = Textaddoutlineandshadow ("Stroke plus shadow Outlineshadow", "Arial", 1, Ccwhite, 4, 200);
    Outlineshadow->setposition (CCP (size.width*0.5, size.height*0.3));
    
    This->addchild (Outlineshadow);
return true; }/* Making text strokes is very simple, after we have written a piece of text, that is to create a cclabelttf, called the body Cclabelttf. Then create 4 Cclabelttf, the color is black, the size is the same as the body Cclabelttf, called the stroke Cclabelttf.
 
 Speaking of which we may already understand, yes, is to put 4 strokes Cclabelttf in the text below the Cclabelttf, respectively, and the text Cclabelttf staggered, so that the effect of stroke is achieved. *string text *fontname text font type *fontsize text size *color3 text color *linewidth the width of the stroke */cclabelttf* Helloworld::tex Taddstroke (const char* string, const char* fontname, float fontsize,const cccolor3b &color3,float linewidth) {//positive
    Text Cclabelttf cclabelttf* Center = cclabelttf::create (String, FontName, fontSize);
    
    Center->setcolor (COLOR3);
    Stroke Cclabelttf on cclabelttf* up = Cclabelttf::create (String, FontName, fontSize);
    Up->setcolor (Ccblack); Up->setposition (CCP (center->getContentsize (). width*0.5, Center->getcontentsize (). height*0.5+linewidth));

    Center->addchild (up,-1);
    Stroke 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* = 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* r = 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; }/* To add a shadow to the text, just lookUnderstand the ... *string text *fontname text font type *fontsize text size *color3 text color *shadowsize Shadow size *shadowopaci Ty Shadow Transparency */cclabelttf* Helloworld::textaddshadow (const char* string, const char* fontname, float fontsize,const cccolor 3 b &color3,float shadowsize,float shadowopacity) {cclabelttf* shadow = cclabelttf::create (String, FontName, FontS
    ize);
    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; }//Both add strokes and add Shadows cclabelttf* Helloworld::textaddoutlineandshadow (const char* string, const char* FontName, float fontSize, Const CCCOLOR3B &color3,float linewidth,float shadowsize,float shadowopacity) {cclabelttf* Center = TextAddStrokE (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; }


Effect as shown:


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.