Change position of a SKSpriteNode that has a physicsBody

前端 未结 7 2267
遇见更好的自我
遇见更好的自我 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条回答
  •  -上瘾入骨i
    2021-02-13 22:23

    I think the problem you are having is that in order for you to control a phsyics body, you need to set it to dynamic.

    self.myStar.physicsBody.dynamic = NO;

    If the dynamic property is set to YES, then the physics engine is in control of it's movement.

    As noted in the comments, setting dynamic to NO shouldn't restrict movement via SKAction or position property. However, if it is set to YES, it's possible that something in the physics engine is affecting your attempt to move the node.

    If you want the movement to not pop, then you need to set a duration higher than zero to your SKAction or it will pop as you have described. Setting duration to 0.0 is the same as just changing the position.

    For example :

    [self runAction:[SKAction moveTo:newPosition duration:1.0]];

    will move the node to the new position over 1 second.

提交回复
热议问题