Cocos2d-x has added a new Label API in the new version, and the difference is that 2. in Version x, three different classes are used to create different text labels. However, the Creation Method of the genie is imitated. A Class creates different forms of text, however, the core content is similar.
This is the new Label class structure.
In the 2.x series, there are three types of texts: TTF, BMFont, and Atlas. In 3.0, the TTF is split into two types. Let's take a look at the creation of these four types of text.
1. Label: createWithTTF
This is how to create a text file in ttf format.
TTFConfig config("fonts/Marker Felt.ttf", 25);auto label1 = Label::createWithTTF(config, "Hello Cocos2d-x");label1->setPosition(Point(size.width/2, size.height*0.8));addChild(label1);
2. Label: createWithSystemFont
This is created using the built-in font, which is similar to TTF in 2.x.
auto label2 = Label::createWithSystemFont("Hello Cocos2d-x", "Consolas", 25);label2->setPosition(Point(size.width / 2, size.height*0.6));addChild(label2);
Note that the first parameter is the text content to be displayed, and the second parameter is the system font.
3. Label: createWithBMFont
Create text using static font files to generate fnt files and corresponding png Images
auto label3 = Label::createWithBMFont("fonts/helvetica-32.fnt", "Hello Cocos2d-x");label3->setPosition(Point(size.width / 2, size.height*0.4));addChild(label3);
4. Label: createWithCharMap
This corresponds to the creation of Atlas in 2.x. However, there are two ways to match the text in the image. One is to directly set the width and height of each font to be obtained, and the other is to use the fnt file, but the principles are the same.
auto label4 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.plist");label4->setPosition(Point(size.width / 2, size.height*0.2));addChild(label4);label4->setString("Cocos2d-x");auto label4 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.png", 48, 66, ' ');label4->setPosition(Point(size.width / 2, size.height*0.2));addChild(label4);label4->setString("Cocos2d-x");
The text effects created in the above four forms.
In addition, there are some alignment parameters in other overload forms of the create FUNCTION, which will not be described here. Besides the different writing forms, the results are the same.