Change position of a SKSpriteNode that has a physicsBody

前端 未结 7 2291
遇见更好的自我
遇见更好的自我 2021-02-13 22:02

I can\'t change the position of a SKSpriteNode by changing

self.position = newPosition;

It doesn\'t work anymore if it has a physicsBody.

7条回答
  •  时光取名叫无心
    2021-02-13 22:17

    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;
    

提交回复
热议问题