Home > Database > Mysql Tutorial > body text

cocos2d 中添加显示文字的三种方式(CCLabelTTF 、CCLabelBMFont

WBOY
Release: 2016-06-07 15:19:56
Original
1267 people have browsed it

CCLabelTTF CCLabelBMFont CCLabelAtlas CCLabelTTF CCLabelTTF 所以, OK, 看下它的使用方法 CCLabelTTF *label = [CCLabelTTF labelWithString:@the stringfontName:@Marker Felt fontSize:21]; [layer addChild:label]; label.position = ccp(100,100);

  • CCLabelTTF
  • CCLabelBMFont
  • CCLabelAtlas

     CCLabelTTF

           CCLabelTTF

           所以,

           OK, 看下它的使用方法

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

           [layer addChild:label];

           label.position = ccp(100,100);

           [label setString:@“change string”]; //修改文字的方法


           这个类使用的是系统中的字体,不需要额外往项目文件中添加字体文件。但如果你有自定义的字体也可以加到项目中,如果无法使用,可以查看一下ccConfig.h中的CC_FONT_LABEL_SUPPORT是否enable了。  

 

     CCLabelBMFont

        CCLabelBMFont  相当于每次改变只改变了图片坐标,而CCLabelTTF要重新渲染.这个类使用之前,需要添加好字体文件,包括一个图片文件 (**.png) 和一个 字体坐标文件 (**.fnt)。

         在 cocos2d的示例项目中有现成的,可以先拿过来练习一下,找的时候注意两个文件的 名称是相同的,只是扩展名不同。

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

           [layer addChild:label];

           label.position = ccp(100,100);

           [label setString:@“change string”];

        fntFile是文件名,写扩展名是 .fnt的那个。下面是 .fnt文件的截图

        ...

        可以看到 .fnt 文件定义了 图像文件的名称,以及每个字符对应的位置信息。

        这个没办法指定字体的字号,但可以用  scale  属性进行缩放来调整大小。就当它是sprite。

 

   CCLabelAtlas        

        如果你用cocos2d项目模板创建过项目,那么你已经看过它的效果了,就是左下角显示帧率的数字。

        因为帧率一直在变,使用CCLabelTTF的话效率太低,因为只是数字所以也犯不上使用 CCLabelBMFont 加载那么大的文字图像,所以使用这个比较合适。

        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”];

 

       在项目文件中,在resourse group里你可以找一下一个叫 fps_images.png的这个图像文件,文件如下:

        

...

       所以,这个只能显示上面这个12个字符,abcd什么的就不行了。

       解释一下参数,

       labelWithString就是字符,

       charMapFile 就是字符的图像文件(这个只要图像文件就可以了),

       itemWidth是每个字符的宽度,

       itemHeight是每个字符的高度,这个不能设错,否则显示的时候可能就不对了。

       最后一个是起始字符,它是使用这个其实字符来对应字符和图像关系的。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!