ios - 与 Sprite Kit 的 bodyWithEdgeLoopF​​romPath 坐标系混淆

标签 ios objective-c sprite-kit

我将此图像作为 SKSpriteNode:

enter image description here

这张图片的尺寸是 394px X 347px。

在代码的后面,我在这个带有图像的 SKSpriteNode 上添加了带有 bodyWithPolygonFromPath 函数的物理体。

我给出的路径是:

CGMutablePathRef shipPath = CGPathCreateMutable();
CGPathMoveToPoint(shipPath, NULL, 0,0);
CGPathAddLineToPoint(shipPath, NULL, 0,347);
CGPathAddLineToPoint(shipPath, NULL, 394, 347);
CGPathAddLineToPoint(shipPath, NULL, 394, 0);

CGPathCloseSubpath(shipPath);

这条路径应该精确地沿着宇宙飞船图像周围的正方形形式的物理路径来检测从上面落下的岩石的勾结。

这是程序输出: enter image description here

下一张截图解释了这个问题:

enter image description here

我哪里错了?我希望我已经很好地解释了这个问题。

最佳答案

查看 documentation 后:

+ (SKPhysicsBody *)bodyWithEdgeLoopFromPath:(CGPathRef)path

path - A Core Graphics path. The points are specified relative to the owning node’s origin. The path must not intersect itself.

SKNode 中的原点由其 anchor 定义,默认情况下为节点中心的 (0.5, 0.5)

为什么是中心?

Sprite 的纹理是根据 anchorPoint 因素相对于节点位置绘制的。它的默认值 (0.5, 0.5) 将纹理放置在节点位置的中心。

你可能想调整 anchor ,但它会改变你不想要的纹理位置。相反,在创建物理体时调整路径。

不过 SKScene 是不同的。对于 SKScene,默认值为 (0, 0),它对应于 View 框架矩形的左下角。要更改为中心,请指定 (0.5, 0.5)

基于:SKPhysicsBody Class Reference , SKNode Class ReferenceSKScene Class Reference

关于ios - 与 Sprite Kit 的 bodyWithEdgeLoopF​​romPath 坐标系混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22127839/

相关文章:

objective-c - -performDragOperation : not called when dragging from dock stack

ios - 大小为零的 UIView 对象是否占用大量内存?

objective-c - 将UISliders值显示为%

ios - Firebase 检索数据并将其传递给变量

ios - 滚动后 tableview 中的数据混淆

swift - 公开拥有属性的对象

ios - SKAction.rotate 不起作用?

ios - SpriteKit 类别位掩码无法正常工作

ios - 任何其他类中的 UINavigationController

ios - UIGestureRecognizer 识别顺序问题