I am new to sprite kit and to develop my skills I am creating a game similar to the popular Flappy Bird. I have created a method which adds in pipes to the scene to be obstacles. I have created static physics bodies for these obstacles by using the bodyWithRectangleOfSize and setting it to the obstacle.size. However the physics bodies are a different size to the the pipe images displayed on screen. What is displayed as empty space on screen, the bird is crashing into. The bird can also fly through sections of the pipe. This seems to be caused by the physics body including the obstacles' position in the size. (Eg the pipe is floating 320px above the bottom of the screen and is 240px height. The physics body is stretching to the entire length of the screen.)
UPDATE: This is no longer the issue. I am now finding the physics bodies on bottom to short and fat and the ones at the top too long. The gap is there but it is in the middle of where a pipe is displayed.
As I said before. I am new to spritekit and this is most likely a basic error on my part. Any help is appreciated. Thank you.
Here is my code to generate each obstacle:
-(void)initialiseObstacles:(int)position
{
//create random height
int random = (arc4random()%250) + 150;
//add bottom pipe and physics body
SKSpriteNode *lowObstacle = [SKSpriteNode spriteNodeWithImageNamed:@"pipe.png"];
lowObstacle.size = CGSizeMake(50, random);
lowObstacle.position = CGPointMake(position + 200, 0);
lowObstacle.anchorPoint = CGPointMake(0,0);
lowObstacle.name = @"lowObstacle";
[self addChild:lowObstacle];
lowObstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:lowObstacle.frame.size];
lowObstacle.physicsBody.dynamic = NO;
lowObstacle.physicsBody.categoryBitMask = obstacleCategory;
//add top pipe & physics body
SKSpriteNode *highObstacle = [SKSpriteNode spriteNodeWithImageNamed:@"pipetop.png"];
highObstacle.anchorPoint = CGPointMake(0,0);
highObstacle.size = CGSizeMake(50, 430-lowObstacle.size.height);
highObstacle.position = CGPointMake(lowObstacle.position.x,lowObstacle.size.height + 130);
highObstacle.name = @"highObstacle";
[self addChild:highObstacle];
highObstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:highObstacle.frame.size];
highObstacle.physicsBody.dynamic = NO;
highObstacle.physicsBody.categoryBitMask = obstacleCategory;
}
This code spawns a pipe every 100 frames and is called by the update:
-(void)spawn{
frames = frames + 1;
if (frames == 100) {
[self initialiseObstacles:200];
frames = 0;
}
}
This code moves the pipes:
-(void)moveObstacles
{
[self enumerateChildNodesWithName:@"lowObstacle" usingBlock:^(SKNode *node, BOOL *stop) {
SKSpriteNode *ob = (SKSpriteNode *)node;
ob.position = CGPointMake(ob.position.x - 1.5, 0);
if (ob.position.x < -ob.size.width) {
[ob removeFromParent];
}
}];
[self enumerateChildNodesWithName:@"highObstacle" usingBlock:^(SKNode *node, BOOL *stop) {
SKSpriteNode *highOb = (SKSpriteNode *)node;
highOb.position = CGPointMake(highOb.position.x - 1.5, highOb.position.y);
if (highOb.position.x < - highOb.size.width) {
[highOb removeFromParent];
}
}];
}