Jadar
Jadar

Reputation: 1690

Why can't I add a Joint to the Scene physicsWorld in SpriteKit

I'm trying to add a joint to the scene physicsWorld property, but I am getting a EXC_BAD_ACCESS. This is the code I'm using.

SKPhysicsJointFixed *fixedJoint = [SKPhysicsJointFixed jointWithBodyA:self.mousePoint.physicsBody bodyB: self.box.physicsBody anchor:touchPoint];

[self.physicsWorld addJoint:fixedJoint]; // Error happens here.

I'm calling it when a touch begins. Does anyone know whats going on?

Upvotes: 5

Views: 1385

Answers (3)

Stoyan
Stoyan

Reputation: 1315

i have simular problem and it was happening because i was creating physics body before i have added the node to the scene.

try this 1 init node 2 add it to the scene 3 attach physics body 4 do whatever with joints

(i was switching 2 and 3 and it hadn't working

Upvotes: 0

zurakach
zurakach

Reputation: 1394

I had same problem. and the problem was:I forgot to add physicsBody to one of the nodes. so i was passing "nil". and this was cause of my error.

try something like this:

node.physicsBody = SKPhysicsBody(rectangleOfSize: node.size)

Upvotes: 0

JohnHanr
JohnHanr

Reputation: 233

i encountered the same problem with you, the answer is very easy, just add the node to the scene before adding it to the physicsworld.

Upvotes: 16

Related Questions