Reputation: 1049
I'm trying to remove some nodes from a parent node once they reach a certain x position. The problem I have is that the parent node is changing x position, but the children are not changing x position inside the parent (but are obviously moving with the parent), so when I put in if node.position.x < 300 . . . (remove node), nothing happens. I tried the below code, but this only work one time and then doesn't remove nodes again, which I'm not 100% sure why it stops working.
func cleanUp() {
let positionX = nodeBase.position.x
nodeBase.enumerateChildNodesWithName("segment", usingBlock: {
node, stop in
if node.position.x - positionX < 300 {
node.removeFromParent()
}
})
}
Can anybody see where I am going wrong with my code, or can you point me in the right direction?
Upvotes: 0
Views: 148
Reputation: 22939
Try the following:
nodeBase.enumerateChildNodesWithName("segment") { node, _ in
if !self.intersectsNode(node) {
node.removeFromParent()
}
}
intersectsNode
returns true
whilst the node
is inside the bounds of SKScene
. Therefore, when intersectsNode
returns false
you know the node is offscreen and you can remove the node.
Upvotes: 1