Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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];

} '''