Responding to Collisions

Great, so we get notified every time the ball collides with targets and bumpers. But how do we tell them apart? We want to do special animations and sound effects for both kinds of bodies, but we only want to increase the user’s score when the ball hits a target.

Let’s go back and revisit the current state of the ‑didBeginContact: method in the RCWMyScene.m file.

07-MorePhysics/step05/PhysicsBall/RCWMyScene.m
 
- (​void​)didBeginContact:(SKPhysicsContact *)contact
 
{
 
NSLog(@​"In -didBeginContact:"​);
 
NSLog(@​"bodyA: %@"​, contact.bodyA);
 
NSLog(@​"bodyB: %@"​, contact.bodyB);
 
}

When two bodies contact each other, how do we know which one is bodyA and which one is bodyB? Unfortunately, we don’t know, and Apple ...

Get Build iOS Games with Sprite Kit now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.