I must be missing something really obvious here. My objective: Create a box in the center of the screen, with a bouncing ball staying inside that box. I'm trying to create an edge-based SKSpriteNode for the bounding box and an volume-based SKShapeNode for the internal ball. THe ball spawns in the center of the box, falls through the box, falls off screen, apparently hits something below the bottom of the screen, and bounces back above the bottom edge a few times before finally disappearing. All the while, didBeginContact never fires.
Question1: why is my ball falling through the visible box, yet contacting something below the edge of the screen? Is my physicsbody misaligned from the actual sprite?
Question2: why is didBeginContact not firing? [Edit, ok this was simple. Forgot to set physicsworld.delegate]
Thanks!
Scene.h
#define MAX_WORLD_CELLS_X 10
#define MAX_WORLD_CELLS_Y 10
#define BGTILE_SIZE 100
typedef enum : uint32_t {
TYPE_SUBSPACE = 1,
TYPE_WORLD = 2,
TYPE_CHILD = 4
} ColliderTypes;
@interface helloScene : SKScene
<SKPhysicsContactDelegate>
{
SKSpriteNode *layerSubSpace,*layerWorld;
}
@end
Scene.m, didBeginContact
-(void) didBeginContact:(SKPhysicsContact *)contact
{
NSLog(@"Function: GameScene.m:didBeginContact");
}
Scene.m, initWithSize
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
CGSize layerSize = CGSizeMake(MAX_WORLD_CELLS_X * BGTILE_SIZE *2, MAX_WORLD_CELLS_Y * BGTILE_SIZE *2);
layerSubSpace = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:layerSize];
//center subSpace on the screen's center
layerSubSpace.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
CGRect subspaceRect = CGRectMake(-layerSubSpace.size.width/2, -layerSubSpace.size.height/2, layerSubSpace.size.width, layerSubSpace.size.height);
layerSubSpace.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:subspaceRect];
layerSubSpace.physicsBody.affectedByGravity = NO;
layerSubSpace.physicsBody.categoryBitMask = TYPE_SUBSPACE;
layerSubSpace.physicsBody.contactTestBitMask = TYPE_WORLD;
layerSubSpace.physicsBody.collisionBitMask = TYPE_WORLD;
[self addChild:layerSubSpace];
SKShapeNode *ball = [[SKShapeNode alloc] init];
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, 0,0, 60, 0, M_PI*2, YES);
ball.path = myPath;
ball.lineWidth = 1.0;
ball.fillColor = [SKColor blueColor];
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:60];
ball.physicsBody.restitution = 1;
ball.physicsBody.categoryBitMask = TYPE_WORLD;
ball.physicsBody.collisionBitMask = TYPE_SUBSPACE;
ball.physicsBody.contactTestBitMask = TYPE_SUBSPACE;
[layerSubSpace addChild:ball];
}
layerSubSpace.xScale = 0.25;
layerSubSpace.yScale = 0.25;
return self;
}