cocos2d-x 3.1.1 学习笔记[1]Label 标签

来源:互联网 发布:excel数据生成图表 编辑:程序博客网 时间:2024/05/20 08:23

开始学习后发现很多东西可以重复使用的,于是便想到应该记录下来学习的过程,中途使用的方法到时候开发的时候可以直接copy过去就好了。

这个星期做完小苹果,下个星期就开始做逗比妹子了。终于要使用期望已久的cocos2d-x 3.1.1了。该死的2.1.4 get away from me.

这个笔记也是一个学习的过程的见证。

好了。不废话了。

3.x里面的变量几乎都可以使用auto这个关键词来代替,再也不要使用神马CCSprite,CCLabel了,看起来就有点像javascript的var关键词,想想还有点小激动呢。

功夫从创建一个Label开始.



//创建一个TTFConfig,在构造函数里面传入ttf的位置和文字的大小    TTFConfig config("PaintBoy.ttf",50);    //通过直接传入ttf的位置来创建一个label    auto ttf0 = Label::createWithTTF("zhouyunxuan", "Paint Boy.ttf", 52);   //输入的字符串可以像c语言一样加上"\n"来进行换行    auto ttf1 = Label::createWithTTF("zhouyunxuan\nxuzhouyu", "Paint Boy.ttf", 52);    //使用TTFConfig来创建一个label    auto ttf2 = Label::createWithTTF(config,"I love cocos2d\nnew line",TextHAlignment::LEFT);        //给相应的ttf设置位置,这个版本已经没有ccp创建点坐标这个函数了。    ttf0->setPosition(Vec2(300, 700));    ttf1->setPosition(Vec2(300, 500));    ttf2->setPosition(Vec2(300, 300));        //添加ttf    this->addChild(ttf0);    this->addChild(ttf1);    this->addChild(ttf2);


运行一下就可以看到效果了:



一开始发现TTFConfig在创建的时候有点奇怪,没有调用任何函数居然可以穿两个参数进去。

点进去才发现TTFConfig是一个结构体,结构体居然还能有构造函数。以后有机会也可以这样展示一下了。


typedef struct _ttfConfig{    //声明变量    std::string fontFilePath;    int fontSize;    GlyphCollection glyphs;    const char *customGlyphs;    bool distanceFieldEnabled;    int outlineSize;    //构造函数    _ttfConfig(const char* filePath = "",int size = 12, const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC,        const char *customGlyphCollection = nullptr,bool useDistanceField = false,int outline = 0)    //给这个机构体的其他变量赋值        :fontFilePath(filePath)        ,fontSize(size)        ,glyphs(glyphCollection)        ,customGlyphs(customGlyphCollection)        ,distanceFieldEnabled(useDistanceField)        ,outlineSize(outline)    {        if(outline > 0)        {            distanceFieldEnabled = false;        }    }}TTFConfig;



通过BMFont创建Label

 /*     *  BMFont是由battle.fnt和battle.png两个文件组成的。.fnt里面记载着相应的字的编码对应的位置。     *  如果字符串里面存在.fnt里面没有记录的文字,则不显示该文字(不会在该文字原来位置留空)    */    auto ttf0 = Label::createWithBMFont("battle.fnt", "012345云轩6789");    ttf0->setPosition(Vec2(300, 300));    this->addChild(ttf0);

显示效果:



根据charMap创建Label

/*     *  createWithCharMap 需要有plist文件或者plist文件里面的值itemHeight(每一个文字的高度) itemWidth(每一个文字的宽度) startCharMap(文字起始ascii码)     *  一般这种图只会用来装饰数字和字母。     */    auto ttf = Label::createWithCharMap("tuffy_bold_italic-charmap.plist");    ttf->setString("0123456789");    ttf->setPosition(Vec2(300, 300));    this->addChild(ttf);

 根据系统文字创建字

/*     *  根据系统字体创建文字     */    auto label = Label::createWithSystemFont("I love cocos", "arial", 32);    label->setPosition(Vec2(300, 300));    this->addChild(label);



 剩下的几个Labelxxx创建方法都可以不使用了,反正Label里面有相对应的创建方法。

    Label::createWithCharMap(<#const std::string &charMapFile#>, <#int itemWidth#>, <#int itemHeight#>, <#int startCharMap#>)

    LabelAtlas::create(<#const std::string &string#>, <#const std::string &charMapFile#>, <#int itemWidth#>, <#int itemHeight#>, <#int startCharMap#>)

    Label::createWithTTF(<#const std::string &text#>, <#const std::string &fontFile#>, <#float fontSize#>)

    LabelTTF::create(<#const std::string &string#>, <#const std::string &fontName#>, <#float fontSize#>)


    Label::createWithBMFont(<#const std::string &bmfontFilePath#>, <#const std::string &text#>)

    LabelBMFont::create(<#const std::string &str#>, <#const std::string &fntFile#>);




0 0
原创粉丝点击