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

Creating the platform


At this point in the game, we have a well-defined clown but he's still stuck endlessly bouncing on the base platform. We now need to write code to add the platforms that will help take the clown higher up in the world. We will create and use a single object of the GameObject class to represent the platform. Let's go over the CreatePlatform function from GameWorld.cpp:

void GameWorld::CreatePlatform()
{
  // platform will be a static body
  b2BodyDef platform_def;
  platform_def.position.Set(0, 0);
  b2Body* platform_body = world_->CreateBody(&platform_def);

  // create platform, set physics body & add to batch node
  platform_ = GameObject::create(this, "cjump01.png");
  platform_->setAnchorPoint(ccp(0, 0.25f));
  platform_->setVisible(false);
  platform_->SetBody(platform_body);
  platform_->SetType(E_GAME_OBJECT_PLATFORM);
  sprite_batch_node_->addChild(platform_, E_LAYER_PLATFORM);
}

We begin by creating a static body positioned at the origin...