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 and then create a new GameObject
for the platform. We set the appropriate anchor point and hide platform_
. We pass the newly created b2Body
object into the platform_
object and set its type to E_GAME_OBJECT_PLATFORM
before adding it to the game's batch node.
I'm sure you noticed how we skipped adding any fixtures to the platform's body. This is because the shape of the platform depends completely on what points the user drags on the screen. Thus, we will add a fixture to the platform's body in the AddPlatform
function when the user has finished touching the screen.
13.59.107.152