Change position of a SKSpriteNode that has a physicsBody

前端 未结 7 2261
遇见更好的自我
遇见更好的自我 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:11

    Yes, you can!

    I'm not sure what exactly you're doing and where exactly you run this code and what the observed effect is that you meant by "can't change position" but changing a node's position always works, whether the node has a physicsBody or not, and whether the physicsBody is dynamic or static. I verified this in a simple test project with dynamic set to both YES and NO, with both circle and edge loop body types, using both position property and move actions.

    You can change the node's position either by setting the position property directly or by using a move action - both variants work. If you use a 0 duration for the move action, it effectively becomes the same as setting the position property.

    So whatever problem you're observing, it's not because you can't generally change a node's position once it has a physicsBody. That's absolutely not the case.

    I'm guessing you may have run into one of the following problems:

    • node is already running a different move action, overriding your position changes
    • you were looking at the wrong node (use logs to verify actual position of the node in question)
    • you change position elsewhere, for example setting the node's position every frame thus invalidating any other position change
    • if it's not one of the above, then possibly something else I couldn't think of ...

提交回复
热议问题