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 – implementing init()


Inside init(), we'll build the game screen, bringing in all the sprites and labels we'll need for the game:

  1. So right after the if statement where we call the super Layer::init method, we add:

    _players = Vector<GameSprite*>(2);
    _player1Score = 0;
    _player2Score = 0;
    _screenSize = Director::getInstance()->getWinSize();
  2. We create the vector where we'll store both players, initialize the score values, and grab the screen size from the singleton, all-knowing Director. We'll use the screen size to position all sprites relatively. Next we will create our first sprite. It is created with an image filename, which FileUtils will take care of loading from the correct folder:

    auto court = Sprite::create("court.png");
    court->setPosition(Vec2(_screenSize.width * 0.5, _screenSize.height * 0.5));
    this->addChild(court);
  3. Get into the habit of positioning sprites with relative values, and not absolute ones, so we can support more screen sizes. And say hello to...