New Label of Cocos2d-x v3.0

New Label of Cocos2d-x v3.0,第1张

概述New Label of Cocos2d-x v3.0 New Label of Cocos2d-x v3.0 What's new of Label in Cocos2d-x v3.0? How to use new Label? Create with SystemFont Create with TTF Create with BMFont Create with CharMap Glow, New Label of Cocos2d-x v3.0 New Label of Cocos2d-x v3.0 What's new of Label in Cocos2d-x v3.0? How to use new Label? Create with SystemFont Create with TTF Create with BMFont Create with CharMap Glow,shadow and outline effects What's new of Label in Cocos2d-x v3.0? Deprecated cclabelTTF,cclabelBMFont,cclabelAtlas. There is only one classLabel since Cocos2d-x v3.0. Glow, shadow and outline effects supported. Using freetype2to generate texture for labels,which improve the speed of creating Fonts and make sure that labels have the same effect on different platforms. How to use new Label? Create with SystemFont

createWithSystemFont() will call the native API by the platform-dependent code. The first 3 parameters are necessarIEs and the rest would have a default value if they were omitted.

  
123
    /** Creates a label with an initial string,Font[Font name or Font file],Font size,dimension in points,horizontal alignment and vertical alignment. **/    static Label* createWithSystemFont(const std::string& text,const std::string& Font,float FontSize,const Size& dimensions = Size::ZERO,TextHAlignment hAlignment = TextHAlignment::left,TextVAlignment vAlignment = TextVAlignment::top);

Create with TTF

CreateWithTTF() uses libfreetype2 to create the Fonts. It would save each char of the strings to the cache that improve the speed of creating Fonts. There are two ways to create a LabelTTF bellows:

  
123456789101112131415161718192021
//First way to create a LabelTTF by TTF filesauto label = Label::createWithTTF("label test","Fonts/Marker Felt.ttf",32);label->setposition(Point(size.wIDth/2,size.height*0.6));this->addChild(label);     //Second way to create a LabelTTF by TTFconfigTTFConfig label_config;//Set the TTF file of TTFConfig,this attribute should not be omittedlabel_config.FontfilePath = "Fonts/Marker Felt.ttf";label_config.FontSize = 32;//Choose the glyhps,which includes DYNAMIC,NEHE,ASCII and CUSTOM Nowlabel_config.glyphs = GlyphCollection::DYNAMIC;//Set the customGlyphs to null since here we don't use the custom glyphs.label_config.customGlyphs = nullptr;//Open or not open distanceFIEldlabel_config.distanceFIEldEnabled = false;label_config.outlinesize = 0;//create the LabelTTF through the label configauto label_two = Label::createWithTTF(label_config,"label test");label_two->setposition(Point(size.wIDth/2,size.height*0.5));this->addChild(label_two);

To kNow what is distanceFIEld, read this paper Distance-field-fonts

Create with BMFont

To create a label with FNT file,we have createWithBMFont()

  
123
//Create a BMFont label,the first parameter is the fnt file,second is your text. The chars of your string should be included in your fnt files,or it can't display nomally.auto bmFont = Label::createWithBMFont("Fonts/gameover_score_num.fnt","123456789");
Create with CharMap

The createWithCharMap() represents the cclabelAtlasin the past. It has a simpler rule compare to the BMFont. It is common used in the number Fonts. We have 3 overrIDed functions in Cocos2d-x v3.0 above to create with char map.

//创建charMap 参数分别为:png图片的路径,每个字符的宽和高,起始字符

  
1234567891011121314151617
// 3 ways to create with char map,according to the 'cclabel.h'static Label * createWithCharMap(const std::string& charMapfile,int itemWIDth,int itemHeight,int startCharMap);static Label * createWithCharMap(Texture2D* texture,int startCharMap);static Label * createWithCharMap(const std::string& pListfile);//create a label with char map.//parameters: PNG picture,wIDth,height,the first char.auto charMap = Label::createWithCharMap("Fonts/tuffy_bold_italic-charmap.png",48,64,' ');charMap->setposition(Point(size.wIDth/2,size.height*0.4));charMap->setString("123456789");this->addChild(charMap);//You can also create the char map by a pListauto charMap2 = Label::createWithCharMap("Fonts/tuffy_bold_italic-charmap.pList");charMap2->setposition(Point(Point(size.wIDth/2,size.height*0.3)));charMap2->setString("123456789");this->addChild(charMap2); 

Glow,shadow and outline effects
  
12345678
//Outline effect,only support for TTFlabel_two->enableOutline(color4B(255,0,255),5);//Glow effect,only support for TTF and the distanceFIEldEnabled must be truelabel_three->enableGlow(color4B(255,255));//Shadow effectlabel_four->enableShadow(color4B(0,255,Size(3,10),0);

distance-field-fonts.png(34.7 kB) @H_553_403@zijian.rao,2014-07-29 09:26

2.png(30.9 kB) @H_553_403@zijian.rao,2014-07-29 09:26

1.png(35.4 kB) @H_553_403@zijian.rao,2014-07-29 09:26

3.png(52.4 kB) @H_553_403@zijian.rao,2014-07-29 09:26

总结

以上是内存溢出为你收集整理的New Label of Cocos2d-x v3.0全部内容,希望文章能够帮你解决New Label of Cocos2d-x v3.0所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1003260.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存