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

kirkbyo
kirkbyo
15,791 Points

Not logging BAM properly

When I try logging BAM in the if statement nothing happens when projectile comes in contact with the nodes, but when I move that same NSLog into the else statement everything happens like it supposed to.

- (void) didBeginContact:(SKPhysicsContact *)contact {
    SKPhysicsBody *firstBody, *secondBody;

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


    if ( firstBody.categoryBitMask == FSCollisionCategoryEnemy && secondBody.categoryBitMask == FSCollisionCategoryProjectile) {
        NSLog(@"Blank");
    } else {
        NSLog(@"BAM!");
    }
}

I have re watched the video to see if I missed anything and downloaded Amit code to compare but I can't seem to find the issue. If anyone can help me to see where I went wrong that would be greatly appreciated.

Ozzie

1 Answer

Rashii Henry
Rashii Henry
16,433 Points

I believe its because when you're using the AND operator you have to understand that both events have to happen at the same time for the if statement to be true. Otherwise it flows into the else statement. Try commenting out one of the events to see if you can play around with it to get it to work.

Does it even log "Blank"?

kirkbyo
kirkbyo
15,791 Points

I did what you said and commented out parts of the if statement. Both firstBody and secondBody are false. I could not find a way Log blank. Would you have any other ideas?