3.4 常用的两种 layer 层 3.7 字体与文本

3.4 常用的两种 layer 层 

//在cocos2d-x中,经常使用到的两种 layer 层 : CCLayer 和 CCLayerColor

    //CCLayer 的创建
    CCLayer* layer = CCLayer::create();
    
    //CCLayerColor 的创建
    CCLayerColor* layerColor = CCLayerColor::create(const cocos2d::ccColor4B &color); //RGBO

    /*
    注意:
    新创建的 CCLayer 和 CCLayerColor 层如果没有手动设置其大小,默认是当前设备的宽高作为 layer 的尺寸
    CCLayer 与 CCLayerColor 虽然默认锚点是 (0.5, 0.5), 坐标(0, 0),但是创建后的层总是充满屏幕的
    */

3.7 字体与文本

 在使用字体的时候需要注意, CCLabelTTF 每调用 setString 改变显示字符串的时候,一个新
的OPENGL纹理将会创建。也就意味着调用 setString 函数和创建一个新的文本一样慢。
∴ 频繁 更新时 建议尽可能不使用 CCLabelTTF 对象, 考虑使用 CCLabelAtlas 或 CCLabelBMFont.

//CCLabelTTF 、CCLabelAtlas 、CCLabelBMFont

    /*1.CCLabelTTF*/
    CCLabelTTF::create(const char* string, const char* fonName, float fontSize);
    //参数1:需要显示的字符串  参数2:字体名称  参数3:字体大小
    CCLabelTTF::create();
    //默认无参创建,默认使用字体类型 Helvetica
    //常用函数为: setString(const char* label);


    /*2.CCLabelAtlas*/  
    //常用函数 setString(const char *label);   setColor(const ccColor3B& color)
    CCLabelAtlas::create(const char* string, const char* charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap);
    //作用:利用一张字体图片资源来创建一个 CCLabelAtlas 对象
    //参数 1:需要显示的字符串
    //参数 2:文字图片资源名称
    //参数 3:每个文字的宽
    //参数 4:每个文字的高
    //参数 5:字体起始标示

    CCLabelAtlas::create(const char* string, const char* fntFile);
    //作用:利用加载字体配置文件,来创建一个 CCLabelAtlas 对象
    //参数 1:需要显示的字符串
    //参数 2:字体配置文件的名称

    /*3.CCLabelBMFont*/
    CCLabelBMFont::create(const char* str, const char* fntFile);
    //参数 1:需要显示的文字
    //参数 2:字体资源文件的名称


    //示例代码
    //---------------CCLabelTTF
    CCLabelTTF* pLabel = CCLabelTTF::create("visionFont", "Thonburi", 24);

    CCLabelTTF* pLabel2 = CCLabelTTF::create();
    pLabel2->setFontSize(24);
    pLabel2->setString("visionFont");

    //---------------CCLabelAtlas
    CCLabelAtlas* label = CCLabelAtlas::create("visionFontAtlas", "testFont.png", 30, 30, ' ');
    label->setColor(ccc3(255, 0, 0));

    CCLabelAtlas* label2 = CCLabelAtlas::create("visionFontAtlas2", "testfont.plist");
    label2->setString("123");

    //---------------CCLabelBMFont
    CCLabelBMFont* labelBM = CCLabelBMFont::create("stand up font", "testFont.fnt");
原文地址:https://www.cnblogs.com/MrGreen/p/3428560.html