Book Image

iOS Game Programming Cookbook

Book Image

iOS Game Programming Cookbook

Overview of this book

Table of Contents (19 chapters)
iOS Game Programming Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Collision detection


We have our game integrated with collectables. Let's see how the spaceship will collect these collectables that is, coins. In character animation, we will be doing the animations on the spaceship and the coins when they collide with each other.

Getting ready

Before moving on to the complex animations to be applied on the entities of the scene, the understanding of actions (that is, SKAction) and update function of scene (SKScene) has to be there. This is so that during updation we can detect the collision between the coin and the spaceship and do some animations on both of them.

How to do it…

The following are the steps involved in detecting the collision and animating both the entities (coin and spaceship):

  1. Write an detectSpaceShipCollisionWithCoins method in which we will enumerate the coin objects.

    - (void)detectSpaceShipCollisionWithCoins
    {
        [self enumerateChildNodesWithName:@"Coin"
                               usingBlock: ^(SKNode *node, BOOL *stop)
         {
         }];
    }
  2. In...