SKshapenode is not responding to Physicsbody

Viewed 4034

I have created a SKShapeNode and I have assigned a physicsBody to it. However, it is not being triggered when there is contact.

Creation of SKShapeNode code:

-(SKShapeNode*)gravityline{
    //SKSpriteNode *lolo=[[SKSpriteNode alloc]init];
    SKShapeNode *lolo = [[SKShapeNode alloc] init];
    CGPoint fff=CGPointMake(ray1.position.x, ray1.position.y);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, fff.x, fff.y);
    CGPathAddLineToPoint(path, 0,rayoriginpoint.x,rayoriginpoint.y );
    CGPathCloseSubpath(path);
    lolo.path = path;
    lolo.name=@"gravityline";
    lolo.strokeColor=[SKColor greenColor];
    lolo.glowWidth=.1;
    CGPathRelease(path);
    lolo.physicsBody=[SKPhysicsBody bodyWithEdgeFromPoint:fff toPoint:rayoriginpoint];
    //lolo.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromPath:path];
    //lolo.physicsBody=[SKPhysicsBody bodyWithPolygonFromPath:path];
    lolo.physicsBody.categoryBitMask=raylightCategory;
    lolo.physicsBody.collisionBitMask=batCategory;
    lolo.physicsBody.contactTestBitMask=batCategory;
    lolo.physicsBody.usesPreciseCollisionDetection=YES;
    lolo.physicsBody.linearDamping=0;
    lolo.physicsBody.restitution=1.0;
    lolo.physicsBody.dynamic=NO;

    return lolo;
}

Here is the trigering code :

- (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 == raylightCategory && secondBody.categoryBitMask==batCategory)
    {
        NSLog(@"Contact with bat have been made");
        [secondBody.node removeFromParent];
    }
}

If anybody has a clue what I did wrong, why the SKShapeNode is not activating the physicsBody, please let me know.

2 Answers
Related