Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit: cannot change node position in contact callback

Tags:

sprite-kit

I have a node with a dynamic physics body. And I would like to make it static and change its position when it comes in contact with another body.

I managed to make the body static with the solution provided in this question: Sprite Kit failing assertion: (typeA == b2_dynamicBody || typeB == b2_dynamicBody)

However if I change the position property of the node in one of the contact callback methods (e.g didBeginContact) the new position is not taken into account.

How could I achieve that?

like image 423
sdabet Avatar asked Apr 02 '14 11:04

sdabet


1 Answers

I believe this is a bug in SpriteKit. (I was able to reproduce this problem with SpriteKit 7.1).

Here is a quick workaround:

- (void) didBeginContact:(SKPhysicsContact *)contact
{
     contact.bodyB.node.position = CGPointMake(newX, newY);
     contact.bodyB.node.physicsBody = contact.bodyB.node.physicsBody; // <-- Add this line
}
like image 66
JKallio Avatar answered Oct 04 '22 10:10

JKallio