Book Image

Cocos2d Game Development Blueprints

By : Jorge Jordán
Book Image

Cocos2d Game Development Blueprints

By: Jorge Jordán

Overview of this book

Table of Contents (15 chapters)
Cocos2d Game Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. In the project navigator, select the Resources group.

  2. Right-click and select Add Files to "SnookerMunk"….

  3. Look for the star.png file in the Resources folder you unzipped.

  4. 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...