Book Image

Cocos2d-X Game Development Blueprints

By : Karan Sequeira
Book Image

Cocos2d-X Game Development Blueprints

By: Karan Sequeira

Overview of this book

Table of Contents (17 chapters)
Cocos2d-x Game Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Moving the player


We had called the scheduleUpdate function in the init function of the Player class. Thus, we define the update function to handle the movement and rotation of the player:

void Player::update(float dt)
{
  CCDrawNode::update(dt);
  CCPoint previous_position = m_obPosition;
  UpdatePosition();
  UpdateRotation(previous_position);
}

We first save the previous position of the player because we need it while setting the rotation. The UpdateRotation function will just use ccpToAngle function to set the rotation of the player with a bit of easing, so we will skip it and discuss only the UpdatePosition function as follows:

void Player::UpdatePosition()
{
  // don't move if speed is too low
  if(ccpLength(speed_) > 0.75f)
  {
    // add speed but limit movement within the boundary
    CCPoint next_position = ccpAdd(m_obPosition, speed_);
    if(RECT_CONTAINS_CIRCLE(game_world_->boundary_rect_, 
      next_position, PLAYER_RADIUS))
    {
      setPosition(next_position);
    ...