Game Development Reference
In-Depth Information
Chapter
4
Physics & Collisions
In this chapter, you'll learn more about editing physics properties with SpriteBuilder and
how to control the player through physics rather than actions. You'll add impenetrable
physics bodies as level borders to prevent the player character from moving outside the
level boundaries.
You'll also learn about how to run code when a collision occurs—in this case, to trigger
game events like ending the current level—and how to set up collision categories and
masks to allow certain physics bodies to pass through each other.
Player Physics
In the previous chapter, you already added a Physics node to the Level1.ccb and made the
player node a child of the newly introduced Physics node. This instance of CCPhysicsNode
acts as the physics world that controls global physics behaviors—most of all, gravity. Any
node that should move and interact like a physics object must be a child or grandchild of a
CCPhysicsNode.
Let's give this a try right away and enable physics for the player.
Enabling Physics for the Player Sprite
First try this: open Level1.ccb, select the player, and try to switch to the Item Physics
tab—the third tab in the Details Inspector on the right. You'll notice it's grayed out—it
can't be selected. This is always the case for Sub File nodes: they do not support physics.
Instead, open the Player.ccb in the Prefabs folder and select the CCSprite root node. Now
you can switch to the Item Physics tab and select the Enable Physics check box. Behind the
scenes, when the CCB is being loaded, the CCBReader will create an instance of the
CCPhysicsBody class and assign it to the node's physicsBody property. When dis-
Search WWH ::




Custom Search