Change position of a SKSpriteNode that has a physicsBody

ios, objective-c, sprite-kit

Solution

I had the same problem. Apparently you cannot explicitly set the position of a Sprite node once it has a PhysicsBody. I solved it by temporarily removing the sprite node's PhysicsBody.

CGFloat yPosition = 400.f;
SKPhysicsBody* goldfishPhysicsBody = _goldfish.physicsBody;
_goldfish.physicsBody = nil;
_goldfish.position = CGPointMake(_goldfish.position.x, yPosition);
_goldfish.physicsBody = goldfishPhysicsBody;

Problem

I can't change the position of a SKSpriteNode by changing ``` self.position = newPosition; ``` It doesn't work anymore if it has a physicsBody. A workaround I got is: ``` self.myStar.physicsBody = nil; [self.myStar changePosition]; self.myStar.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.myStar.size.width/2]; ``` Or ``` [self runAction:[SKAction moveTo:newPosiion duration:0.0]]; ``` But #2 isn't smooth. It needs to pop up on another position without a moving action.

Original source

Related problems