Creating sprites with physics
If you thought that the only way to view bodies was using the debug draw mode, you were wrong. In the current version of Cocos2d, CCNode
has a property called physicsBody
where we can link a physical body to the node.
This feature makes it as easy to create a sprite that responds to physical stimulations as declaring a common sprite and setting the CCPhysicalBody
instance to it. Let's prove this with an example.
We are going to create a star, so paste the following lines in the variables declaration section in GameScene.m
:
// Declare a sprite CCSprite *_star;
We want the star to have an image so:
In the project navigator, select the
Resources
group.Right-click and select Add Files to "SnookerMunk"….
Look for the
star.png
file in theResources
folder you unzipped.Be sure that Copy items into destination group's folder (if needed) is selected and click on Add.
Now add the following lines at the end of the init
method just before return self;
:
// Initialize...