如何在视图中获取子精灵位置

时间:2015-06-07 23:59:42

标签: swift sprite-kit skspritenode sknode

我试图根据答案herethis

在视图中获取子精灵位置
for child in platformGroupNode.children {
    CGPoint p = [child.superview convertPoint:child.center toNode:self.view ]
    println(p)
}

但我不确定如何在SKSpriteNode儿童和SKNode家长处使用此功能。

我也试过这个没有运气

for child in platformGroupNode.children {
    var pos = child.position
    var viewPos = convertPoint(pos, toNode: self.parent!)
    if viewPos.x < 0 {
        println("Out of screen")
    }
}

1 个答案:

答案 0 :(得分:5)

你快到了,你需要使用的是:

let positionInScene = self.convertPoint(child.position, fromNode: platformGroupNode)
// self in this case is your SKScene, you don't need self here but, IMO, it 
// makes it easier to understand what's converting what.

或等效的是:

let positionInScene = platformGroupNode.convertPoint(child.position, toNode: self)