Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Game with Sprite Kit Artificial Intelligence and Sound Adding Background Music

Jared Watkins
Jared Watkins
10,756 Points

Some spaceDogs are taking several hits (collisions) before being removed. With both projectiles and the ground

The projectile collides with the spaceDog and debris comes out from it, but it doesn't get removed. It can take 1 to 6 or more collisions before being removed the sound effect is not triggered. The sounds effect is not triggered until the spaceDog is actually removed, and the NSLog isn't triggered until it is removed.

Also, the collisions with the groundNode are behaving similarly. when the spaceDog and groundNode collide, debris will appear, but the dog may still pass through the groundNode. The NSLog for this collision never gets triggered.

Here's the physics contact part from GamePlayScene.m


  • (void) didBeginContact:(SKPhysicsContact *)contact {

    SKPhysicsBody *firstBody, *secondBody;

    if ( contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask ) { firstBody = contact.bodyA; secondBody = contact.bodyB; } else { firstBody = contact.bodyA; secondBody = contact.bodyB; }

    if ( firstBody.categoryBitMask == collisionCategoryEnemy && secondBody.categoryBitMask == collisionCategoryProjectile ) { NSLog(@"BAM!");

    SpaceDogNode *spaceDog = (SpaceDogNode *)firstBody.node;
    ProjectileNode *projectile = (ProjectileNode *)secondBody.node;
    [self runAction:self.explodeSFX];
    [spaceDog removeFromParent];
    [projectile removeFromParent];

    } else if (firstBody.categoryBitMask == collisionCategoryEnemy && secondBody.categoryBitMask == collisionCategoryGround ) { NSLog(@"Hit Ground");

    [self runAction:self.damageSFX];
    SpaceDogNode *spaceDog = (SpaceDogNode *)firstBody.node;
    [spaceDog removeFromParent];


    [self createDebrisAtPosition:contact.contactPoint];

} '''