I'm really confused on why I am getting an EXC_BAD_ACCESS (code=1, address=0x1c) at [world addJoint:pinJoin];.
JointTest.m
#import "JointTest.h"
@implementation JointTest
-(SKNode *)initWithWorld:(SKPhysicsWorld *)pWorld{
if(self = [super init]){
world = pWorld;
[self attachBodies];
}
return self;
}
-(void)attachBodies{
SKSpriteNode * spriteA = [[SKSpriteNode alloc]initWithImageNamed:@"rocket"];
SKSpriteNode * spriteB = [[SKSpriteNode alloc]initWithImageNamed:@"rocket"];
SKPhysicsBody * bodyA = [SKPhysicsBody bodyWithRectangleOfSize:spriteA.size];
SKPhysicsBody * bodyB = [SKPhysicsBody bodyWithRectangleOfSize:spriteB.size];
spriteA.position = CGPointMake(150, 300);
spriteB.position = CGPointMake(150, 300 + spriteB.size.height/2);
[self addChild:spriteA];
[self addChild:spriteB];
bodyA.dynamic = NO;
spriteA.physicsBody = bodyA;
spriteB.physicsBody = bodyB;
SKPhysicsJointPin * pinJoin = [SKPhysicsJointPin jointWithBodyA:spriteA.physicsBody bodyB:spriteB.physicsBody anchor:spriteA.position];
[world addJoint:pinJoin];
}
@end
JointTest.h
#import <SpriteKit/SpriteKit.h>
@interface JointTest : SKNode{
SKPhysicsWorld * world;
}
-(SKNode *)initWithWorld:(SKPhysicsWorld *)pWorld;
@end
In the SKScene
JointTest * test = [[JointTest alloc]initWithWorld:self.physicsWorld];
[self addChild:test];
What confuses me is moving the code in the attachBodies method into the scene and calling the the addJoint method with the scene's physicsWorld works great. For instance:
SKSpriteNode * spriteA = [[SKSpriteNode alloc]initWithImageNamed:@"rocket"];
SKSpriteNode * spriteB = [[SKSpriteNode alloc]initWithImageNamed:@"rocket"];
SKPhysicsBody * bodyA = [SKPhysicsBody bodyWithRectangleOfSize:spriteA.size];
SKPhysicsBody * bodyB = [SKPhysicsBody bodyWithRectangleOfSize:spriteB.size];
spriteA.position = CGPointMake(150, 300);
spriteB.position = CGPointMake(150, 300 + spriteB.size.height/2);
[self addChild:spriteA];
[self addChild:spriteB];
bodyA.dynamic = NO;
spriteA.physicsBody = bodyA;
spriteB.physicsBody = bodyB;
SKPhysicsJointPin * pinJoin = [SKPhysicsJointPin jointWithBodyA:spriteA.physicsBody bodyB:spriteB.physicsBody anchor:spriteA.position];
[self.physicsWorld addJoint:pinJoin];
I've been pulling my hair out for a few hours over this so if anyone has an idea I would greatly appreciate it. Thanks!