Book Image

Cocos2d-x by Example: Beginner's Guide

By : Roger Engelbert
Book Image

Cocos2d-x by Example: Beginner's Guide

By: Roger Engelbert

Overview of this book

Table of Contents (19 chapters)
Cocos2d-x by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – creating bitmap font labels


Creating a bitmap font is somewhat similar to creating a batch node.

  1. Continuing with our createGameScreen method, add the following lines to the score label:

    _scoreDisplay = Label::createWithBMFont("font.fnt", "0");
    _scoreDisplay->setAnchorPoint(Vec2(1,0.5));
    _scoreDisplay->setPosition(Vec2 (_screenSize.width * 0.8f, _screenSize.height * 0.94f));
    this->addChild(_scoreDisplay);

    And then add a label to display the energy level, and set its horizontal alignment to Right:

    _energyDisplay = Label::createWithBMFont("font.fnt", "100%", TextHAlignment::RIGHT);
    _energyDisplay->setPosition(Vec2 (_screenSize.width * 0.3f, _screenSize.height * 0.94f));
    this->addChild(_energyDisplay);
  2. Add the following line for an icon that appears next to the _energyDisplay label:

    auto icon = Sprite::createWithSpriteFrameName("health_icon.png");
    icon->setPosition( Vec2(_screenSize. width * 0.15f,  _screenSize.height * 0.94f) );
    _gameBatchNode->addChild(icon...