Search code examples
swiftsprite-kitskspritenodesknode

How to get a child sprite position in the view


I am attempting to get the child sprites position in the view based on the answer here and this

for child in platformGroupNode.children {
    CGPoint p = [child.superview convertPoint:child.center toNode:self.view ]
    println(p)
}

However I am unsure how to use this with SKSpriteNode Children and SKNode Parents.

I have also tried this with no luck

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

Solution

  • You're nearly there, what you need to use is:

    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.
    

    Or an equivalent would be:

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