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 player


Open up the Player.cpp class.

  1. The _player object is created through a static method that uses our blank.png file to texture the sprite. That method also makes a call to initPlayer, and this is what you should type for that method:

    void Player::initPlayer () {
        this->setAnchorPoint(Vec2(0.5f, 1.0f));
        this->setPosition(Vec2(_screenSize.width * 0.2f, _nextPosition.y));
        
        _height = 228;
        _width = 180;
        this->setTextureRect(Rect(0, 0, _width, _height));
        this->setColor(Color3B(255,255,255));
    }

    The _player object will have its registration point at the top of the sprite. The reason behind this top center anchor point has much more to do with the way the _player object will be animated when floating, than with any collision logic requirements.

  2. Next comes setFloating:

    void Player::setFloating (bool value) {
        
        if (_floating == value) return;
        
        if (value && _hasFloated) return;
        
        _floating = value;
        ...