When I run the code below I get a SIGABRT error, and I have looked everywhere and have tried everything to fix it to no avail. I am trying to use SKShapeNode and make it into a physics object with collisions.
Look at my comment below. The line and the cart are set to collide and end game according to my code but none of this happens, and rather they completely ignore each other. Is there something wrong in my code?
Code:
static const uint32_t rockCategory = 0x1 << 0;
static const uint32_t mineCartCategory = 0x1 << 1;
static const uint32_t lineCategory = 0x1 << 1;
@interface MyScene() <SKPhysicsContactDelegate, StartGameLayerDelegate, GameOverLayerDelegate>
{
NSTimeInterval _dt;
float bottomScrollerHeight;
BOOL _gameStarted;
BOOL _gameOver;
CGMutablePathRef pathToDraw;
SKShapeNode *lineNode;
StartGameLayer* _startGameLayer;
GameOverLayer* _gameOverLayer;
int _score;
}
#pragma mark - Touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
lineNode = [SKShapeNode node];
lineNode.path = pathToDraw;
lineNode.strokeColor = [SKColor redColor];
lineNode.zPosition = 125;
lineNode.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:lineNode.path];
lineNode.physicsBody.categoryBitMask = lineCategory;
lineNode.physicsBody.contactTestBitMask = mineCartCategory;
lineNode.physicsBody.collisionBitMask = 0;
lineNode.physicsBody.affectedByGravity = NO;
[self addChild:lineNode];
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
lineNode.path = pathToDraw;
}
#pragma mark - Collisions
- (void)rock:(SKSpriteNode *)pillar didCollideWithCart:(SKSpriteNode *)cart
{
[self showGameOverLayer];
[lineNode removeFromParent];
}
- (void)line:(SKSpriteNode *) lines didCollideWithCart:(SKSpriteNode *) cart
{
[self showGameOverLayer];
[lineNode removeFromParent];
}
- (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 & rockCategory) != 0 &&
(secondBody.categoryBitMask & mineCartCategory) != 0)
{
[self rock:(SKSpriteNode *) firstBody.node didCollideWithCart:(SKSpriteNode *) secondBody.node];
}
else if ((firstBody.categoryBitMask & lineCategory) != 0 &&
(secondBody.categoryBitMask & mineCartCategory) != 0)
{
[self line:(SKSpriteNode *) firstBody.node didCollideWithCart:(SKSpriteNode *) secondBody.node];
}
}
Look at my comment below. The line and the cart are set to collide and end game according to my code but none of this happens, and rather they completely ignore each other. Is there something wrong in my code?
Last edited: