Reputation: 3092
I have been fiddling with this topic for about three months now. I thought it was just a temporary bug Apple would fix soon and postponed it to the end of development. Now my game is almost finished and I still could not find a good workaround:
If I have a SKShapeNode
and add a physicsbody to it, I cannot use the setPosition:(CGPoint)
method to change the position anymore. My object gets always placed at (0,0) of its parent view.
I have tried setting the position inside the didSimulatePhysics
method, to make sure all physic calculation were done.
I tried using SKAction
to move the node and other suggestions from similar questions here. None worked as desired.
The only (kind of) working solution for me is to use this workaround method:
- (void)setPositionWorkaround:(CGPoint)p {
CGVector vel = self.physicsBody.velocity;
CGFloat angVel = self.physicsBody.angularVelocity;
SKPhysicsBody *temp = self.physicsBody;
self.physicsBody = nil;
[self setPosition:p];
[self setPhysicsBody:temp];
[self.physicsBody setVelocity:vel];
[self.physicsBody setAngularVelocity:angVel];
}
Strangely this method works only once. Every additional time I call this method the position is set to the first call parameter even though there is no data left from the initial call. I checked the parameters and the coordinate systems countless times.
Did someone run into the same problem and found a working solution?
Upvotes: 2
Views: 464
Reputation: 3092
After a long time fiddling with this problem I finally found the solution for it.
So, since a relatively recent update on SpriteKit (3-4 months back) I cannot move any SKNode
subclass that has a SKPhysicsBody
attached to it. setPosition:(CGPoint)position
and [SKAction move…]
both do not work.
In my case removing the SKPhysicsBody
and setting the position did not work, because I was using a pool that recycled my SKShapeNode
s for performance reasons. The pool did not remove the attached SKPhysicBodies
though.
So: If you have this problem make sure ALL SKPhysicBody
objects are removed from the node and childnodes.
Upvotes: 1