Reputation: 59
I'm trying to remove some sprites when they hit the bottom. It works if I don't check the name but it removes my background too. When I try to add the name to the if
it crashes.
func checkifbotsreachbottom() {
for child in self.children {
if (child.position.y == 0 && child.name == "botone") {
self.removeChildrenInArray([child])
}
}
}
This crashes, but if I remove the child.name
part it doesn't.
Upvotes: 0
Views: 2890
Reputation: 149
Try with removeFromParent()
Im using xCode 6.0.1 and it works with this code fine for me. Performance is constant.
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
for child in children{
if(child.position.y < 0 && child.name == "ship"){
child.removeFromParent()
}
}
}
Upvotes: 0
Reputation: 154533
self.children
returns [AnyObject]
. If you cast it to [SKNode]
everything should be fine:
func checkifbotsreachbottom(){
for child in self.children as [SKNode] {
if (child.position.y == 0 && child.name == "botone") {
self.removeChildrenInArray([child])
}
}
}
Upvotes: 1