Game Development Reference
In-Depth Information
How to do it…
The following are the steps involved in detecting the collision and animating both the entit-
ies (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 that enumeration, determine that the frame of the spaceship and the frame of any
coin intersects with the help of
CGRectIntersectsRect()
.
[self enumerateChildNodesWithName:@"Coin"
usingBlock: ^(SKNode *node,
BOOL *stop)
{
if
(CGRectIntersectsRect(self.spaceShipSprite.frame,
node.frame))
{
}
}];
3. When a collision is detected, inform the scene that a coin has collided with the
spaceship by a function called
spaceShipCollidedWithCoin
.
[self spaceShipCollidedWithCoin:node];
After all this, the
detectSpaceShipCollisionWithCoins
method looks like this: