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

Detecting collisions with enemies


In this section, we are going to implement a solution to detect collisions between our main character and the enemies to manage what happens when the zombie hits a human.

In GameScene.m, add the following block of code at the end of the gamePadPushButton method:

// Detect collision
for (Human *human in _arrayOfHumans){
        // Detect collision
        if (CGRectIntersectsRect(_zombie.boundingBox, human.humanSprite.boundingBox) && !_humanCollisionDetected
            && _zombie.position.y <= (human.humanSprite.position.y + 12)
            && _zombie.position.y >= (human.humanSprite.position.y - 12)) { // anchorpoint
            _humanCollisionDetected = TRUE;
                // Managing collisions
                [self manageCollisionForHuman:human];
                break;
        }
    }

In this loop, we're just checking whether the zombie bounding box intersects with some of the humans. Also, we check that the y value is inside...