wanksta11
wanksta11

Reputation: 59

beginner xcode swift sprite kit - remove sprite by name crash

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

Answers (2)

Joe
Joe

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

vacawama
vacawama

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

Related Questions