Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change position of a SKSpriteNode that has a physicsBody

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.

like image 311
Norman Avatar asked Jan 05 '14 19:01

Norman


1 Answers

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;
like image 119
user3440752 Avatar answered Sep 18 '22 13:09

user3440752