EXC_BAD_ACCESS SKPhysicsWorld

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

 #import <SpriteKit/SpriteKit.h> @interface JointTest : SKNode{ SKPhysicsWorld * world; } -(SKNode *)initWithWorld:(SKPhysicsWorld *)pWorld; @end 

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!

+7
ios ios7 xcode5 sprite-kit
source share
1 answer

Passing into the physics world on init is not a good design. Instead, instantiate the class normally, and then send it the attachBodies message from where you created the JointTest instance. You can then access the physical world through self.scene.physicsWorld instead of going through the world and maintaining an extraneous link to it.

Instead of this:

 JointTest * test = [[JointTest alloc]initWithWorld:self.physicsWorld]; [self addChild:test]; 

Do it:

 JointTest * test = [JointTest node]; [self addChild:test]; [test attachBodies]; 

Note that attachBodies dispatched after addChild , because before addChild JointTest scene property will still be zero.

I am sure this will also solve your problem, although this is an assumption :

You create a JointTest and immediately add two sprites with joints to it, but at the moment the JointTest instance has not yet been added to the node graph (via addChild). Thus, JointTest and its two child nodes are simply not yet “registered” with the physical world to begin with, which could lead to a crash.

+5
source share

All Articles