Sprite Kit更改锚点,但保持Physicalbody居中 [英] Sprite Kit change anchorpoint but keep physicsbody centered
问题描述
我有许多不同的Sprite节点,它们的Physibody大小与对象相同.对于定位,我需要更改节点的锚点,但是这也会更改物理物体的位置.有没有办法使物理物体的锚点保持居中?为物理体使用路径是没有选择的,因为我有很多不同的对象.
I have a lot of different sprite nodes with a physicsbody the same size as the object. For positioning I need to change the anchorpoint of the node, but this changes the position of the physicsbody as well. Is there a way to keep the anchorpoint for the physicsbody centered? Using a path for the physicsbody is no option because I have so many different objects.
推荐答案
anchorPoint
是纯可视属性,它定义如何相对于节点位置绘制纹理.更改anchorPoint
不会影响物理实体,它仍然位于节点的position
上.
The anchorPoint
is a purely visual property, it defines how the texture is drawn relative to the node's position. The physics body remains unaffected by changing the anchorPoint
, it remains centered on the node's position
.
从某种意义上讲,物理物体确实已经保持在小精灵位置的中心.通过更改anchorPoint,您仅更改了显示精灵纹理的位置,并且我相信您假设物理物体将以该sprite的anchorPoint为中心.并非如此,因为每个节点都可以具有一个物理主体,但是只有少数节点(精灵,场景,视频)具有anchorPoint属性.
So in a sense, the physics body does remain centered on the sprite's position already. By changing the anchorPoint you merely changed where the sprite's texture is displayed, and I believe you assumed the physics body would center on the sprite's anchorPoint. It does not, for one every node can have a physics body but only few nodes (sprite, scene, video) have an anchorPoint property.
解决此问题的最佳方法是创建子画面图像,以便始终假定物理主体位于图像的中心.在图像周围保留透明边框,以确保图像尺寸始终相同,并且身体位置正确居中.
The best way to fix this is to create your sprite images so that the physics body is always assumed to be centered on the image. Leave transparent borders around the image to ensure the image size is always the same and the position of the body properly centered.
还可以使用SKPhysicsBody bodyWithRectangleOfSize:center:
初始值设定项来定义主体的中心点,并将其与子画面的anchorPoint相匹配.但这是棘手且适得其反的,因为即使进行最小的更改,您也必须不断重新调整身体和Sprite anchorPoint的位置.
You can also use SKPhysicsBody bodyWithRectangleOfSize:center:
initializer to define the center point of the body and to match it up with the sprite's anchorPoint. But this is tricky and counterproductive, as you'll have to constantly realign body and sprite anchorPoint if you make even the smallest change.
除此之外,最好不要离开anchorPoint,尤其是在物理方面.
Other than that it's best to leave the anchorPoint alone, especially with physics.
这篇关于Sprite Kit更改锚点,但保持Physicalbody居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!