Three methods for adding display text in cocos2d (CCLabelTTF, CCLabelBMFont

Source: Internet
Author: User
Okay. Let's take a look at its usage method: CCLabelTTF * label [CCLabelTTFlabelWithString: @ thestringfontName: @ MarkerFeltfontSize: 21]; [layeraddChild: label]; label. positionccp (100,100 );

CCLabelTTF CCLabelBMFont cclcclcclcclcclcclcclcclelttf. So, OK. Check its usage. CCLabelTTF * label = [constraint labelWithString: @ the stringfontName: @ Marker Felt fontSize: 21]; [layer addChild: label]; label. position = CPP (100,100 );

  • CCLabelTTF
  • CCLabelBMFont
  • CCLabelAtlas

CCLabelTTF

CCLabelTTF

So,

OK. Check its usage.

CCLabelTTF * label = [CCLabelTTF labelWithString: @ "the string" fontName: @ "Marker Felt" fontSize: 21];

[Layer addChild: label];

Label. position = ccp (100,100 );

[Label setString: @ "change string"]; // Method for modifying text


This class uses fonts in the system. You do not need to add a font file to the project file. However, if you have a Custom font, you can add it to the project. If you cannot use it, check whether CC_FONT_LABEL_SUPPORT in ccConfig. h is enable.

CCLabelBMFont

CCLabelBMFontIt is equivalent to changing only the coordinates of the image each time, and the CCLabelTTF must be re-rendered. before using this class, you need to add a font file, including an image file (**. png) and a font coordinate file (**. fnt ).

In the example project of cocos2d, there is a ready-made item. You can take it for practice first. Note that the names of the two files are the same, but the extensions are different.

CCLabelBMFont * label = [CCLabelBMFont labelWithString: @ "the string" fntFile: @ "konqa32-hd.fnt"];

[Layer addChild: label];

Label. position = ccp (100,100 );

[Label setString: @ "change string"];

FntFile is the file name and the write extension is. fnt. Below is the. fnt File

...

The. fnt file defines the name of the image file and the location information corresponding to each character.

The font size cannot be specified, but the scale attribute can be used to adjust the font size. That's when it's sprite.

CCLabelAtlas

If you have created a project using the cocos2d Project template, you have already seen its effect, that is, the frame rate number displayed in the lower left corner.

Because the frame rate keeps changing, it is too inefficient to use CCLabelTTF because it is only a number, so it is not recommended to use CCLabelBMFont to load such a large text image. Therefore, this method is suitable.

CCLabelAtlas * label = [CCLabelAtlas labelWithString: @ "12" charMapFile: @ "fps_images.png" itemWidth: 12 itemHeight: 18 startCharMap: '.'];

[Layer addChild: label];

Label. position = ccp (100,100 );

[Label setString: @ "34"];

In the project file, you can find the image file fps_images.png in resourse group. The file is as follows:

...

Therefore, this can only display the above 12 characters, abcd or something will not work.

Explain the parameters,

LabelWithString is a character,

CharMapFile is the character image file (this only requires the image file ),

ItemWidth is the width of each character,

ItemHeight is the height of each character, which cannot be set incorrectly; otherwise, it may be incorrect when displayed.

The last character is the starting character. It uses this character to indicate the relationship between the character and the image.

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.