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 Physics and Collision Contact Detection using SKPhysicsContactDelegate

Shaun Kelly
Shaun Kelly
5,648 Points

Not receiving a notification that the contact occurred ?

GameScene.h
#import <SpriteKit/SpriteKit.h>

@interface GameScene : SKScene <SKPhysicsContactDelegate>

@end
GameScene.m
    //GREENSHAPE
    SKShapeNode *greenShape = [[SKShapeNode alloc] init];

    CGMutablePathRef pathTwo = CGPathCreateMutable();
    CGPathMoveToPoint(pathTwo, NULL, 0, 0);
    CGPathAddLineToPoint(pathTwo, NULL, 70, 40);
    CGPathAddLineToPoint(pathTwo, NULL, 0, -81);
    CGPathAddLineToPoint(pathTwo, NULL, 0, 0);
    greenShape.path = pathTwo;
    greenShape.fillColor = [SKColor greenColor];
    greenShape.strokeColor = [SKColor greenColor];
    greenShape.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:pathTwo];
    greenShape.physicsBody.affectedByGravity = NO;
    greenShape.physicsBody.dynamic = NO;
    greenShape.physicsBody.categoryBitMask = CollisionCategoryGreenShape;
    greenShape.physicsBody.contactTestBitMask = CollisionCategoryGreenBall;
    greenShape.physicsBody.collisionBitMask = CollisionCategoryGreenBall;

    [square addChild:greenShape];



-(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 == CollisionCategoryGreenShape &&
        secondBody.categoryBitMask == CollisionCategoryGreenBall) {

        NSLog(@"Contact");
    }
}
@end
Balls.m
#import "Balls.h"
#import "Utility.h"
@implementation Balls
+(instancetype) ballOfType:(Balltype)type {


    Balls *ball;


    if (type == BalltypeBlue) {
        ball = [self spriteNodeWithImageNamed:@"blueBall"];


    }
    else if (type == BalltypeGreen) {
        ball = [self spriteNodeWithImageNamed:@"greenBall"];
        ball.physicsBody.categoryBitMask = CollisionCategoryGreenBall;
        ball.physicsBody.contactTestBitMask = CollisionCategoryGreenShape;
        ball.physicsBody.collisionBitMask = CollisionCategoryGreenShape;
        ball.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(30, 30)];
        ball.physicsBody.affectedByGravity = NO;




    }
    else {
        ball = [self spriteNodeWithImageNamed:@"orangeBall"];


    }


    ball.zPosition = 1;
    //[ball setUpPhysics];
    ball.name = @"ball";
    return ball;




}

-(void) setUpPhysics {

    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(30, 30)];
    self.physicsBody.affectedByGravity = NO;



}


Utility.h 
#import <Foundation/Foundation.h>

typedef NS_OPTIONS(uint32_t, CollisionCategory) {
    CollisionCategoryGreenBall   = 1 << 0,
    CollisionCategoryGreenShape  = 1 << 1,

};

@interface Utility : NSObject
@end
Shaun Kelly
Shaun Kelly
5,648 Points

the <SKPhysicsContactDelegate> isn't showing up in the example above for some reason