O'Reilly logo

Build iOS Games with Sprite Kit by Josh Smith, Jonathan Penn

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required