Cocos2d3.0RC2 Label study Note 1

Source: Internet
Author: User

Cocos2d3.0RC2 official offers us a lot of examples, here we learn Label, character bag official project is there, I also passed a copy here: http://download.csdn.net/detail/qqmcy/7245621


LabelAtlasTest

bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }        Size visibleSize = Director::getInstance()->getVisibleSize();    Point origin = Director::getInstance()->getVisibleOrigin();    auto label1 = LabelAtlas::create("LabelAtLas  Test", "tuffy_bold_italic-charmap.plist");    addChild(label1, 0, 0);    label1->setPosition(Point(10, 100));    label1->setOpacity(200);        auto label2 = LabelAtlas::create("678904327423567", "tuffy_bold_italic-charmap.plist");    addChild(label2,0,1);    label2->setPosition(Point(10, 200));    label2->setOpacity(132);    _time = 0;        schedule(schedule_selector(HelloWorld::step));            return true;}void HelloWorld::step(float dt){    _time += dt;    char string[12] = {0};    sprintf(string, "%2.2f Test",_time);        auto label1 = (LabelAtlas*)getChildByTag(0);    label1->setString(string);        auto label2 = (LabelAtlas*)getChildByTag(1);    sprintf(string, "%d",(int)_time);    label2->setString(string);        }


LabelAtlasColorTest


bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }        Size visibleSize = Director::getInstance()->getVisibleSize();    Point origin = Director::getInstance()->getVisibleOrigin();    auto label1 = LabelAtlas::create("LabelAtLas  Test", "tuffy_bold_italic-charmap.png",48,64,' ');        addChild(label1, 0, 0);    label1->setPosition(Point(10, 100));    label1->setOpacity(200);        auto label2 = LabelAtlas::create("678904327423567", "tuffy_bold_italic-charmap.png",48,64,' ');    addChild(label2,0,1);    label2->setPosition(Point(10, 200));    label2->setOpacity(132);    label2->setColor(Color3B::GREEN);            auto fade = FadeOut::create(1.0f);    auto fade_in = fade->reverse();    auto cb = CallFunc::create(CC_CALLBACK_0(HelloWorld::actionFinishCallback, this));    auto seq = Sequence::create(fade,fade_in,cb, NULL);    auto repeat = RepeatForever::create(seq);    label2->runAction(repeat);    schedule(schedule_selector(HelloWorld::step));                _time = 0;                return true;}void HelloWorld::actionFinishCallback(){    CCLOG("Action finished");}void HelloWorld::step(float dt){    _time += dt;    char string[12] = {0};    sprintf(string, "%2.2f Test",_time);    auto label1 = (LabelAtlas*)getChildByTag(0);    label1->setString(string);        auto label2 = (LabelAtlas*)getChildByTag(1);    sprintf(string, "%d", (int)_time);    label2->setString( string );    }

LabelTTFAlignment


bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }        auto s = Director::getInstance()->getWinSize();       auto ttf0 = LabelTTF:: create("Alignment 0\nnew line", "Helvetica", 32,Size(256, 32),TextHAlignment::LEFT);    ttf0->setPosition(Point(s.width / 2, s.height / 6 * 2));    ttf0->setAnchorPoint(Point::ANCHOR_MIDDLE);    this->addChild(ttf0);            auto ttf1 = LabelTTF:: create("Alignment 0\nnew line", "Helvetica", 32,Size(256, 32),TextHAlignment::CENTER);    ttf1->setPosition(Point(s.width / 2, s.height / 6 * 3));    ttf1->setAnchorPoint(Point::ANCHOR_MIDDLE);    this->addChild(ttf1);            auto ttf2 = LabelTTF:: create("Alignment 0\nnew line", "Helvetica", 32,Size(256, 32),TextHAlignment::RIGHT);    ttf2->setPosition(Point(s.width / 2, s.height / 6 * 4));    ttf2->setAnchorPoint(Point::ANCHOR_MIDDLE);    this->addChild(ttf2);    _time = 0;        return true;}

Effect:



LabelBMFont


bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }           auto col = LayerColor::create(Color4B(128, 128, 128, 255));    addChild(col, -10);        auto label1 = LabelBMFont::create("Test", "bitmapFontTest2.fnt");    label1->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);    addChild(label1, 0, 10);        auto fade = FadeOut::create(1.0f);    auto fade_in = fade->reverse();    auto seq = Sequence::create(fade,fade_in, NULL);    auto repeat =  RepeatForever::create(seq);    label1->runAction(repeat);        auto label2 = LabelBMFont::create("Testedddddd", "bitmapFontTest2.fnt");    label2->setPosition(Point(100, 200));    label2->setAnchorPoint(Point::ANCHOR_MIDDLE);    label2->setColor(Color3B::RED);    addChild(label2, 0, 11);        auto tint = Sequence::create(TintTo::create(1, 255, 0, 0),TintTo::create(1, 0, 255, 0),TintTo::create(1, 0, 0, 255), NULL);    label2->runAction(RepeatForever::create(tint));                auto label3 = LabelBMFont::create("ghjkl", "bitmapFontTest2.fnt");    label3->setAnchorPoint(Point::ANCHOR_TOP_RIGHT);    addChild(label3, 0, 12);    label3->setPosition(Point(720, 100));           schedule(schedule_selector(HelloWorld::step));            return true;}void HelloWorld::step(float dt){    _time += dt;    //std::string string;    char string[15] = {0};    sprintf(string, "%2.2f Test j", _time);    //string.format("%2.2f Test j", _time);        auto label1 = (LabelBMFont*) getChildByTag(10);    label1->setString(string);        auto label2 = (LabelBMFont*) getChildByTag(11);    label2->setString(string);        auto label3 = (LabelBMFont*) getChildByTag(12);    label3->setString(string);}

Effect:





bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }           auto label = LabelBMFont::create("Bitmap Font Atlas", "bitmapFontTest.fnt");    addChild(label);        auto s = Director::getInstance()->getWinSize();    label->setPosition(Point(s.width / 2, s.height / 2));    label->setAnchorPoint(Point::ANCHOR_MIDDLE);        auto rotate = RotateBy::create(2, 360);    auto rot_4ever = RepeatForever::create(rotate);        auto scale = ScaleBy::create(2, 1.5f);    auto scale_back = scale->reverse();    auto scale_seq = Sequence::create(scale,scale_back, NULL);    auto scale_4ever = RepeatForever::create(scale_seq);        auto jump = JumpBy::create(0.5f, Point::ZERO, 60, 1);    auto jump_4ever = RepeatForever::create(jump);        auto fade_out = FadeOut::create(1);    auto fade_in = FadeIn::create(1);    auto seq = Sequence::create(fade_out,fade_in, NULL);    auto fade_4ever = RepeatForever::create(seq);        label->runAction(fade_4ever);    auto label2 = LabelBMFont::create("00.0", "bitmapFontTest.fnt");    addChild(label2, 0, 13);    label2->setPosition(Point(s.width / 2, 80));        label2->runAction(rot_4ever->clone());        schedule(schedule_selector(HelloWorld::step),0.1f);            return true;}void HelloWorld::step(float dt){    _time += dt;    //std::string string;    char string[15] = {0};    sprintf(string, "%2.2f Test j", _time);    //string.format("%2.2f Test j", _time);        auto label1 = (LabelBMFont*) getChildByTag(13);    label1->setString(string);        }

Effect:




<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cCBjbGFzcz0 = "p1"> bitmapFontTest4.fnt


     auto label = LabelBMFont::create("dfghjk", "bitmapFontTest4.fnt");    addChild(label);        auto s = Director::getInstance()->getWinSize();        label->setPosition( Point(s.width/2, s.height/2) );    label->setAnchorPoint( Point::ANCHOR_MIDDLE );



BitmapFontTest5.fnt

 auto s = Director::getInstance()->getWinSize();        LabelBMFont* label = NULL;    label = LabelBMFont::create("FaFeFiFoFu", "bitmapFontTest5.fnt");    addChild(label);    label->setPosition( Point(s.width/2, s.height/2+50) );    label->setAnchorPoint( Point::ANCHOR_MIDDLE ) ;        label = LabelBMFont::create("fafefifofu", "bitmapFontTest5.fnt");    addChild(label);    label->setPosition( Point(s.width/2, s.height/2) );    label->setAnchorPoint( Point::ANCHOR_MIDDLE );        label = LabelBMFont::create("aeiou", "bitmapFontTest5.fnt");    addChild(label);    label->setPosition( Point(s.width/2, s.height/2-50) );    label->setAnchorPoint( Point::ANCHOR_MIDDLE );






 auto s = Director::getInstance()->getWinSize();        LabelBMFont* label = nullptr;    label = LabelBMFont::create("Blue", "bitmapFontTest5.fnt");    label->setColor( Color3B::BLUE );    addChild(label);    label->setPosition( Point(s.width/2, s.height/4) );    label->setAnchorPoint( Point::ANCHOR_MIDDLE );        label = LabelBMFont::create("Red", "bitmapFontTest5.fnt");    addChild(label);    label->setPosition( Point(s.width/2, 2*s.height/4) );    label->setAnchorPoint( Point::ANCHOR_MIDDLE );    label->setColor( Color3B::RED );        label = LabelBMFont::create("G", "bitmapFontTest5.fnt");    addChild(label);    label->setPosition( Point(s.width/2, 3*s.height/4) );    label->setAnchorPoint( Point::ANCHOR_MIDDLE );    label->setColor( Color3B::GREEN );    label->setString("Green");



for( int i=0 ; i < 100;i ++ )    {        char str[6] = {0};        sprintf(str, "-%d-", i);        auto label = LabelBMFont::create(str, "bitmapFontTest.fnt");        addChild(label);                auto s = Director::getInstance()->getWinSize();                auto p = Point( CCRANDOM_0_1() * s.width, CCRANDOM_0_1() * s.height);        label->setPosition( p );        label->setAnchorPoint(Point::ANCHOR_MIDDLE);    }







 Size s;        // Left    auto label1 = Label::createWithBMFont("bitmapFontTest3.fnt", " Multi line\nLeft");    label1->setAnchorPoint(Point(0,0));    addChild(label1, 0, 12);        s = label1->getContentSize();    CCLOG("content size: %.2fx%.2f", s.width, s.height);            // Center    auto label2 = Label::createWithBMFont( "bitmapFontTest3.fnt", "Multi line\nCenter");    addChild(label2, 0, 13);        s= label2->getContentSize();    CCLOG("content size: %.2fx%.2f", s.width, s.height);        // right    auto label3 = Label::createWithBMFont("bitmapFontTest3.fnt", "Multi line\nRight\nThree lines Three");    label3->setAnchorPoint(Point(1, 1));    addChild(label3, 0, 14);        s = label3->getContentSize();    CCLOG("content size: %.2fx%.2f", s.width, s.height);        label1->setPosition(Point(50, 50));    label2->setPosition(Point(100, 400));    label3->setPosition(Point(300, 300));





 auto s = Director::getInstance()->getWinSize();        // LabelBMFont    auto label1 = Label::createWithBMFont("konqa32.fnt", "TESTING RETINA DISPLAY");    addChild(label1);    label1->setPosition(Point(s.width/2, s.height/2));





 auto s = Director::getInstance()->getWinSize();            auto layer = LayerColor::create(Color4B(128, 128, 128, 255));    addChild(layer, -10);        // LabelBMFont    auto label1 = Label::createWithBMFont("futura-48.fnt", "TESTING RETINA DISPLAY");    addChild(label1);    label1->setPosition(Point(s.width/2, s.height/2));





bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }           auto listener = EventListenerTouchAllAtOnce::create();    listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);    listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);    listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);        _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);        auto size = Director::getInstance()->getWinSize();        this->_labelShouldRetain = Label::createWithBMFont("markerFelt.fnt", LongSentencesExample,TextHAlignment::CENTER,size.width / 1.5);    this->_labelShouldRetain->retain();        this->_arrowsBarShouldRetain = Sprite::create("arrowsBar.png");    this->_arrowsBarShouldRetain->retain();    this->_arrowsShouldRetain = Sprite::create("arrows.png");    this->_arrowsShouldRetain->retain();        MenuItemFont::setFontSize(20);    auto longSentences = MenuItemFont::create("Long Flowing Sentences", CC_CALLBACK_1(HelloWorld::stringChanged, this));    auto lineBreaks = MenuItemFont::create("Short Sentences With Intentional Line Breaks", CC_CALLBACK_1(HelloWorld::stringChanged, this));    auto mixed = MenuItemFont::create("Long Sentences Mixed With Intentional Line Breaks", CC_CALLBACK_1(HelloWorld::stringChanged, this));        auto stringMenu = Menu::create(longSentences,lineBreaks,mixed, NULL);    stringMenu->alignItemsVertically();        longSentences->setColor(Color3B::RED);    _lastSentenceItem = longSentences;    longSentences->setTag(LongSentences);    lineBreaks->setTag(LineBreaks);    mixed->setTag(Mixed);            MenuItemFont::setFontSize(30);        auto left = MenuItemFont::create("Left", CC_CALLBACK_1(HelloWorld::alignmentChanged, this));    auto center = MenuItemFont::create("Center", CC_CALLBACK_1(HelloWorld::alignmentChanged, this));    auto right = MenuItemFont::create("Right", CC_CALLBACK_1(HelloWorld::alignmentChanged, this));        auto alignmentMenu = Menu::create(left,center,right, NULL);    center->setColor(Color3B::RED);    _lastAlignmentItem = center;    left->setTag(LeftAlign);    center->setTag(CenterAlign);    right->setTag(RightAlign);        this->_labelShouldRetain->setPosition(Point(size.width / 2, size.height / 2));    this->_arrowsBarShouldRetain->setVisible(false);    float arrowsWidth = (ArrowsMax - ArrowsMin) * size.width;    this->_arrowsBarShouldRetain->setScaleX(arrowsWidth / this->_arrowsBarShouldRetain->getContentSize().width);        this->snapArrowsToEdge();        stringMenu->setPosition(Point(size.width/2, size.height - menuItemPaddingCenter));    alignmentMenu->setPosition(Point(size.width/2, menuItemPaddingCenter+15));        this->addChild(this->_labelShouldRetain);    this->addChild(this->_arrowsBarShouldRetain);    this->addChild(this->_arrowsShouldRetain);    this->addChild(stringMenu);    this->addChild(alignmentMenu);                return true;}void HelloWorld::stringChanged(cocos2d::Ref *sender){    auto item = (MenuItemFont*)sender;    item->setColor(Color3B::RED);    this->_lastAlignmentItem->setColor(Color3B::WHITE);    this->_lastAlignmentItem = item;        switch(item->getTag())    {        case LongSentences:            this->_labelShouldRetain->setString(LongSentencesExample);            break;        case LineBreaks:            this->_labelShouldRetain->setString(LineBreaksExample);            break;        case Mixed:            this->_labelShouldRetain->setString(MixedExample);            break;                    default:            break;    }        this->snapArrowsToEdge();}void HelloWorld::snapArrowsToEdge(){    this->_arrowsShouldRetain->setPosition(Point(this->_labelShouldRetain->getPosition().x + this->_labelShouldRetain->getContentSize().width/2,                                                 this->_labelShouldRetain->getPosition().y));}void HelloWorld::onTouchesBegan(const std::vector
 
  & touches, cocos2d::Event  *event){    auto touch = touches[0];    auto location = touch->getLocationInView();        if (this->_arrowsShouldRetain->getBoundingBox().containsPoint(location))    {        _drag = true;        this->_arrowsBarShouldRetain->setVisible(true);    }}void HelloWorld::onTouchesEnded(const std::vector
  
   & touches, cocos2d::Event  *event){    _drag = false;    this->snapArrowsToEdge();        this->_arrowsBarShouldRetain->setVisible(false);}void HelloWorld::onTouchesMoved(const std::vector
   
    & touches, cocos2d::Event  *event){    if (! _drag)    {        return;    }        auto touch = touches[0];    auto location = touch->getLocationInView();        auto winSize = Director::getInstance()->getWinSize();        this->_arrowsShouldRetain->setPosition(Point(MAX(MIN(location.x, ArrowsMax*winSize.width), ArrowsMin*winSize.width),                                                 this->_arrowsShouldRetain->getPosition().y));        float labelWidth = fabs(this->_arrowsShouldRetain->getPosition().x - this->_labelShouldRetain->getPosition().x) * 2;        this->_labelShouldRetain->setMaxLineWidth(labelWidth);}void HelloWorld::alignmentChanged(cocos2d::Ref *sender){    auto item = static_cast
    
     (sender);    item->setColor(Color3B::RED);    this->_lastAlignmentItem->setColor(Color3B::WHITE);    this->_lastAlignmentItem = item;        switch(item->getTag())    {        case LeftAlign:            this->_labelShouldRetain->setAlignment(TextHAlignment::LEFT);            break;        case CenterAlign:            this->_labelShouldRetain->setAlignment(TextHAlignment::CENTER);            break;        case RightAlign:            this->_labelShouldRetain->setAlignment(TextHAlignment::RIGHT);            break;                    default:            break;    }        this->snapArrowsToEdge();}
    
   
  
 




LabelTTFLongLineWrapping

 auto size = Director::getInstance()->getWinSize();        TTFConfig ttfConfig("arial.ttf",14);    auto label1 = Label::createWithTTF(ttfConfig, LongSentencesExample, TextHAlignment::CENTER,size.width);    label1->setPosition( Point(size.width/2, size.height/2) );    label1->setAnchorPoint(Point(0.5, 1.0));    addChild(label1);



 auto size = Director::getInstance()->getWinSize();        // Long sentence    TTFConfig ttfConfig("wt021.ttf",18,GlyphCollection::DYNAMIC);    std::string text = FileUtils::getInstance()->getStringFromFile("commonly_used_words.txt");    auto label = Label::createWithTTF(ttfConfig,text, TextHAlignment::CENTER, size.width);    label->setPosition( Point(size.width/2, size.height/2) );    addChild(label);


Related Article

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.