Sprite Kit 更改锚点但保持物理主体居中 [英] Sprite Kit change anchorpoint but keep physicsbody centered

查看:10
本文介绍了Sprite Kit 更改锚点但保持物理主体居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多不同的精灵节点,它们的物理体与对象大小相同.对于定位,我需要更改节点的锚点,但这也会更改物理体的位置.有没有办法让物理体的锚点居中?为物理体使用路径是没有选择的,因为我有很多不同的对象.

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,您只更改了精灵纹理的显示位置,我相信您假设物理主体将以精灵的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: 初始化程序来定义主体的中心点并将其与精灵的锚点匹配.但这很棘手且适得其反,因为即使进行最小的更改,您也必须不断地重新调整 body 和 sprite 锚点.

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.

除此之外,最好不要管锚点,尤其是物理方面.

Other than that it's best to leave the anchorPoint alone, especially with physics.

这篇关于Sprite Kit 更改锚点但保持物理主体居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆