0

1) I set the delegate protocol on the SKScene header:

@interface WorldScene : SKScene <SKPhysicsContactDelegate>

2) I set delegate to the physics world:

- (id) init
{
    self = [super init];
    if (self)
    {
        self.physicsWorld.gravity = CGVectorMake(0,0);
        self.physicsWorld.contactDelegate = self;
        self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    }
    return self;
}

3) I set my bitmasks:

static const uint32_t playerCategory  =  0x1 << 0;
static const uint32_t wallsCategory  =  0x1 << 1;
static const uint32_t endCategory  =  0x1 << 2;

4) I make my player:

    SKSpriteNode *player = [[SKSpriteNode alloc] initWithColor:[SKColor blueColor] size:CGSizeMake(PLAYERSIZE, PLAYERSIZE)];
player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];
player.name = @"player";
player.physicsBody.dynamic = YES;
player.physicsBody.categoryBitMask = playerCategory;
player.physicsBody.collisionBitMask = wallsCategory;
player.physicsBody.contactTestBitMask = endCategory;

5) I make my end tile:

SKSpriteNode *tile = [[SKSpriteNode alloc] initWithColor: [SKColor whiteColor] size:CGSizeMake(TILESIZE,TILESIZE)];

if ([cell isEnd]){
    tile.color = [SKColor greenColor];
    tile.name = @"endTile";
    tile.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: tile.size];
    NSLog(@"END TILE OF SIZE %f x %f", tile.size.width, tile.size.height);
    tile.physicsBody.dynamic = YES;
    tile.physicsBody.affectedByGravity = NO;
    tile.physicsBody.categoryBitMask = endCategory;
    tile.physicsBody.collisionBitMask = 0;
    tile.physicsBody.contactTestBitMask = playerCategory;

}

6) But this never gets called! :(

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

    NSLog(@"Touched!");
    [self removeAllChildren];
}

I'm making a maze game. The player currently collides with all the walls fine, but I want the game to end when it reaches the last tile. Currently, the contact event isn't being called. I don't really know what's wrong, I have my debugger output set to displaying all output (and in any case all the children should be getting removed if it reaches the event anyway).

Whyyyyyy

1 Answers1

0

SOLVED -- turns out init was never being called, so the delegate was not actually being set. Moved all the init code to - (void) didMoveToView:(SKView *)view and it began working just fine.