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 – coding the Block object


Once again a static method, create, will use blank.png to create our Block sprite. Only this time, we don't actually change the texture rectangle for Block inside create:

  1. The Block object is properly textured inside the setupBlock method:

    void Block::setupBlock (int width, int height, int type) {
        
        _type = type;
        
        _width = width * _tileWidth;
        _height = height * _tileHeight;
        
        this->setAnchorPoint(Vec2(0,0));
        this->setTextureRect(Rect(0, 0, _width, _height));

    A Block object's appearance will be based on its type, width, and height.

    The Block sprite's registration point is set to top left. And we finally change the Block object's texture rectangle size here.

  2. Then we set the Block object's color based on type:

        switch (type) {
            
            case kBlockGap:
                this->setVisible(false);
                return;
                
            case kBlock1:
                
                this->setColor(Color3B(200,200,200));...