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 placeholder sprites


So let me show you how to do that:

  1. Go ahead and download the 4198_06_START_PROJECT.zip file if you haven't done so already.

  2. When you open the project in Xcode, you will see all the classes we'll need for the game, and we'll go over them in a second. But for now, just go to GameLayer.cpp.

  3. Scroll down to the last createGameScreen method and add the following lines:

    auto quickSprite = Sprite::create("blank.png");
    quickSprite->setTextureRect(Rect(0, 0, 100, 100));
    quickSprite->setColor(Color3B(255,255,255));
    
    quickSprite->setPosition(Vec2(_screenSize.width * 0.5, _screenSize.height * 0.5));
    this->addChild(quickSprite);

    And that's it. The sprite is created with a texture called blank.png. This is a 1 x 1 pixel white square you will find in the Resources folder. Then we set the size of the sprite's texture rectangle to 100 x 100 pixels (setTextureRect), and fill it with a white color (setColor). By resizing the texture rectangle, we in effect...