Cocos2d-x3.0中改进的标签

来源:互联网 发布:淘宝装修工具2.1下载 编辑:程序博客网 时间:2024/05/06 13:01

转自:http://blog.csdn.net/huang_hws/article/details/18504377

在cocos2dx 3.0发布之前,有CCLabelTTF, CCLabelBMFont和CCLabelAtlas三种文本标签。

cocos2dx 3.0添加了一种新的文本标签。这种标签通过使用freetype来使它在不同的平台上有相同的视觉效果。由于使用更快的缓存代理,它的渲染也将更加快速。你将可以LabelTTF和LabelBMFont放一边如果你使用新的文本标签,因为它同时还提供了绘边、阴影等特性。

变动:

1.去掉类、结构体和宏的CC前缀。去掉诸如m、p等的匈奴利命名规则;

2.将成员函数的某些参数和返回值改掉。例如将char改成string, unisigned int改成long等;

3.改掉一些枚举类型的命名和属性值;

4.添加常量修饰到某些成员函数上,如float getFontSize() const;提升程序的鲁棒(为啥不叫撸管子呢)性

旧版的CCLabel的一些近似的改变

1.继承关系的比较

在3.0版本中,所有的类都移除掉CC前缀,同时也删除了CCCopty类,如:

  

PS:如果没理解错的话,官网这两个图的脚注是弄反了,自己擅自都它们改了过来,哈哈。

2.水平对齐和垂直对齐的定义改变了

在 v2.x版本中:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static CCLabelTTF * create(const char *string, const char *fontName, float fontSize,  
  2.                                      const CCSize& dimensions, CCTextAlignment hAlignment,   
  3.                                      CCVerticalTextAlignment vAlignment);  
而在v3.0的版本中则是:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static LabelTTF * create(const std::string& string, const std::string& fontName, float fontSize,  
  2.                                const Size& dimensions, TextHAlignment hAlignment,  
  3.                                TextVAlignment vAlignment);  
从以上的例子可以看出更新版本中函数字义的变化

首先就是不再使用CC前缀。

然后就是char数组改成string类型(string类型相对来说更加高级并且便于使用,而char数组虽然有效率,但也更容易出错)。

另外,最后两个参数的类型也改变了(文件的水平和垂直对齐方式)。不过它们的效果与前一个版本大致相同,只有名字按下列的方式改变了:

 cocos2d-x-2.xcocos2d-x-3.xhorizontal alignmenttypedef enum{kCCTextAlignmentLeft,kCCTextAlignmentCenter,kCCTextAlignmentRight,} CCTextAlignment;enum class TextHAlignment{LEFT,CENTER,RIGHT};vertical alignmenttypedef enum{kCCVerticalTextAlignmentTop, kCCVerticalTextAlignmentCenter,kCCVerticalTextAlignmentBottom,} CCVerticalTextAlignment;enum class TextVAlignment{TOP,CENTER,BOTTOM};旧版本的CCLabel的其它变化:

LabelBMFont移除了现在的函数

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. CCBMFontConfiguration* getConfiguration() const;  
在v3.0版本中不能再使用这个函数来获得配置信息(FNT文件的配置属性了)。

拓展

新的标签类

先看一看标签类的继承图:



上面的图表中,LabelTextFormatProtocal也是一个新的类,它类似于LabelProtocol,它们都是提供存取字符串的纯虚基类。

下面是继承图:

 

Label类中的静态成员函数:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static Label* createWithTTF(const std::string& label, const std::string& fontFilePath, int fontSize, int lineSize = 0, TextHAlignment alignment = TextHAlignment::CENTER, GlyphCollection glyphs = GlyphCollection::NEHE, const char *customGlyphs = 0);      
  2. static Label* createWithBMFont(const std::string& label, const std::string& bmfontFilePath, TextHAlignment alignment = TextHAlignment::CENTER, int lineSize = 0);  
CreateWithTTF()用于直接通过TTF文件创建标签。

它创建标签的方法和创建LabelTTF相似。唯一的不同是后者是用字体名来创建标签,而前者则直接使用ttf文件来创建。

下面代码分别使用两种方法来创建LabelTTF和Label:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. auto label1 = LabelTTF::create("Creating label through LabelTTF class by file name""myFontName", 24);  
  2. label1->setPosition(Point(origin.x, origin.y + visibleSize.height - label1->getContentSize().height));  
  3. label1->setAnchorPoint(Point(0.0f, 0.0f));  
  4. this->addChild(label1);  
  5.   
  6. auto label2 = Label::createWithTTF("Create label through Label class by .ttf file","fonts/myFontName.ttf", 32);  
  7. label2->setPosition(Point(origin.x, origin.y + visibleSize.height - 80));  
  8. label2->setAlignment(TextHAlignment::LEFT);  
  9. label2->setAnchorPoint(Point(0.0f, 0.0f));  
  10. this->addChild(label2);  
运行程序你将得到类似下图的结果:


LabelTTF类是使用系统的字体来生成的。

可以使用下面的方法来设置文本的对齐方法:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. label2->setAlignment(TextHAlignment::LEFT);   //Set the text level alignment is aligned to the left  
CreateWithBMFont()通过FNT文件来创建标签,它类似于LabelBMFont。下面的代码段用不同的方法创建LabelBMFont和Label:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. auto label3 = LabelBMFont::create("Create label through LabelBMFont class by .fnt file""fonts/helvetica-32.fnt");  
  2. label3->setAnchorPoint(Point(0.0f, 0.0f));  
  3. label3->setPosition(Point(origin.x, origin.y + visibleSize.height - 120));  
  4. addChild(label3);  
  5.   
  6. auto label4 = Label::createWithBMFont("Create label through Label class by .fnt file""fonts/konqa32.fnt");  
  7. label4->setAnchorPoint(Point(0.0f, 0.0f));  
  8. label4->setPosition(Point(origin.x, origin.y + visibleSize.height - 160));  
  9. addChild(label4 );  
运行结果类似于:


使用LabelBMFont类,相关的字体文件(包括.png文件和.fnt文件)必须添加到项目中。Label类也一样。

.fnt文件包括了相关图片文件的名字、编码、座标轴、各字符的长宽。

你可以像下面的代码一样调用成员函数来改变label4的一些属性:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. auto size = label4->getLabelContentSize();  
  2. CCLOG("Label content size: %.2fx%.2f", size.width, size.height);  
  3. auto lineWidth = label4->getMaxLineWidth();  
  4. CCLOG("lineWidth: %.2f", lineWidth);  
  5. auto s = label4->getStringLenght();  
  6. CCLOG("string lenght:%.2f", s);  
  7. label4->setScale(2.0f);                         //Changes both X and Y scale factor of the label4  
  8. label4->setColor(Color3B::GREEN);               //Changes the color  
  9. label4->setOpacity(127);                        //Changes the opacity.   
  10. auto CChar = (Sprite*)label4->getLetter(0);     //get the frist letter of label, each letter of the label can be treated like an CCSprite.  
  11. auto jump = JumpBy::create(0.5f, Point::ZERO, 60, 1);  
  12. auto jump_4ever = RepeatForever::create(jump);  
  13. CChar->runAction(jump_4ever);  
运行程序观察不同:




PS:自己翻译给自己看的,请不要吐槽翻译得太烂,个人觉得比较机器翻译得好

0 0
原创粉丝点击