【发布时间】:2015-04-11 20:25:08
【问题描述】:
如果包含场景的anchorPoint 为 (0.5, 0.5),将点转换为绝对坐标的正确方法是什么?从概念上讲,我们将 anchorPoint 设置为 (0.5, 0.5),然后添加一个位置为 (0, 0) 的子项。假设设备是 5S,转换应该产生 (160, 240)。
我们正在使用 Swift。
【问题讨论】:
标签: ios swift sprite-kit
如果包含场景的anchorPoint 为 (0.5, 0.5),将点转换为绝对坐标的正确方法是什么?从概念上讲,我们将 anchorPoint 设置为 (0.5, 0.5),然后添加一个位置为 (0, 0) 的子项。假设设备是 5S,转换应该产生 (160, 240)。
我们正在使用 Swift。
【问题讨论】:
标签: ios swift sprite-kit
在您的场景中添加SKNode。然后根据场景的帧设置它的位置。示例代码:
var node = SKNode()
node.anchorPoint = CGPointMake(0,0)
node.position = CGPointMake(-scene.frame.width/2, -scene.frame.height/2)
scene.addChild(node)
在这个节点上添加你的精灵,你现在可以得到绝对位置了~
【讨论】:
您可以使用SKNode提供的以下methods来获取节点树中的相对点。
convertPoint:toNode:
convertPoint:fromNode:
使用上面Carrl的方法添加一个SKNode,可以得到绝对点数如下:
var relativeNode = SKNode()
relativeNode.anchorPoint = CGPointMake(0,0)
relativeNode.position = CGPointMake(-scene.frame.width/2, -scene.frame.height/2)
scene.addChild(relativeNode)
var absolutePoint = scene.convertPoint(someNode.position, toNode: relativeNode)
//This will result in {160, 240} on a 5S.
这些方法考虑了节点的各种锚点。
【讨论】: