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

Adding and removing platforms


The user can drag across the screen any number of times but we will always use just one platform, ignoring the previously drawn platform. For this, we define the AddPlatform function in GameWorld.cpp:

void GameWorld::AddPlatform(CCPoint start, CCPoint end)
{
  // ensure the platform has only one edge shaped fixture
  if(platform_->GetBody()->GetFixtureList())
    return;

  // create and add a new fixture based on the user input
  b2EdgeShape platform_shape;
  platform_shape.Set(b2Vec2(SCREEN_TO_WORLD(start.x), 
    SCREEN_TO_WORLD(start.y)), b2Vec2(SCREEN_TO_WORLD(
    end.x), SCREEN_TO_WORLD(end.y)));
  platform_->GetBody()->CreateFixture(&platform_shape, 0);
}

The first statement calls the GetFixtureList function of the b2Body class, which returns a pointer to the first b2Fixture object in the array of fixtures the body has applied to it. This if condition will ensure that the platform body never gets more than one fixture.

Then, we proceed to...