【问题标题】:Get absolute coordinates of point from scene with anchorPoint of (0.5, 0.5)?从(0.5,0.5)的anchorPoint获取场景中点的绝对坐标?
【发布时间】: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


    【解决方案1】:

    在您的场景中添加SKNode。然后根据场景的帧设置它的位置。示例代码:

        var node = SKNode()
        node.anchorPoint = CGPointMake(0,0)
        node.position = CGPointMake(-scene.frame.width/2, -scene.frame.height/2)
        scene.addChild(node)
    

    在这个节点上添加你的精灵,你现在可以得到绝对位置了~

    【讨论】:

    • 我正在为类似的问题而苦苦挣扎,我想知道这个解决方案;但第二个问题是:SKNode 没有 anchorPoint 属性
    【解决方案2】:

    您可以使用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.
    

    这些方法考虑了节点的各种锚点。

    【讨论】:

      猜你喜欢
      • 2018-03-13
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2013-01-27
      • 1970-01-01
      相关资源
      最近更新 更多