我将此图像作为 SKSpriteNode:
这张图片的尺寸是 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);
这条路径应该精确地沿着宇宙飞船图像周围的正方形形式的物理路径来检测从上面落下的岩石的勾结。
这是程序输出:
下一张截图解释了这个问题:
我哪里错了?我希望我已经很好地解释了这个问题。
最佳答案
查看 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 Reference和 SKScene Class Reference
关于ios - 与 Sprite Kit 的 bodyWithEdgeLoopFromPath 坐标系混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22127839/