Book Image

Mastering Cocos2d Game Development

By : Alex Ogorek
Book Image

Mastering Cocos2d Game Development

By: Alex Ogorek

Overview of this book

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

Handling collisions in Chipmunk


It's relatively easy to handle collisions within Cocos2d when using Chipmunk (as is pretty much everything else in Cocos2d). That is why so many developers use Cocos2d. To do so, we need to do a few different steps so that Cocos2d can properly detect and handle our collisions.

Setting the collision delegate

The delegate pattern is a common way to handle messages sent by a class to any of its potential parent classes. For example, if you want to use UITableView (which is the standard table in UIKit), you must set the delegate of the table view to the class you're adding it to so that when the table view tries to refresh the data in the table, it knows which class' methods to call.

That being said, we need to tell MainScene that it will be a delegate for our collision handler, so open up MainScene.h and add CCPhysicsCollisionDelegate to the @interface line. This will allow the CCPhysicsNode object to set collision detection events on our MainScene class:

@interface...