I am really confused by why I get EXC_BAD_ACCESS (code = 1, address = 0x1c) in [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
In SKScene
JointTest * test = [[JointTest alloc]initWithWorld:self.physicsWorld]; [self addChild:test];
I am confused by moving the code in the attachBodies method to the scene and calling the addJoint method with the PhysicsWorld scene works fine. For example:
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 pulled my hair for several hours, so if anyone has an idea, I would really appreciate it. Thanks!
ios ios7 xcode5 sprite-kit
Eric
source share