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

1-star challenge – collision detection between Dr. Fringe and the UFO


I'm sure you're now an expert at managing collisions, so I think we should detect collisions between our scientist hero and the spaceships or the game will lack realism.

Try to detect these collisions so we can't fly over the whole airspace and make the scientist blink as we did with the yeti in the previous chapter.

The solution

To achieve this challenge, you will need to take advantage of detectCollisions. How it's built makes our task very easy. As we want to detect collisions with UFOs, we will include our code inside the _arrayUFOs loop, and the best place is after this line:

[_arrayLaserGreen removeObjectsInArray:_lasersGreenToRemove];

Paste the following block of code there:

        // Detect ufo-scientist collision
        if (CGRectIntersectsRect(ufo.boundingBox, _scientist.boundingBox)) {
            
            // Blink actions for both fire and scientist
            CCAction *actionDrBlink = [CCActionBlink actionWithDuration...