Reputation: 585
In my game, I have a method bringing down sprites. When the user loses (gameStarted == false) , I need a way to remove all these nodes I've brought into the scene. I've tried to remove these nodes two ways, by simply saying ball.removeFromParent(), and also using the removeBalls function at the bottom. Both methods stopped future balls from entering the scene, but didn't remove the ones already in existence. If anybody has a way of doing this, I'd really appreciate it.
override func didMoveToView(view: SKView) {
var create = SKAction.runBlock({() in self.createTargets()})
var wait = SKAction.waitForDuration(2)
var createAndWaitForever = SKAction.repeatActionForever(SKAction.sequence([create, wait])
self.runAction(createAndWaitForever)
}
func createTargets() {
ball = SKSpriteNode(imageNamed:"blueBlue")
let randomx = Int(arc4random_uniform(370) + 165)
ball.position = CGPoint(x: randomx, y: 1400)
addChild(ball)
let moveDown = SKAction.moveBy(CGVector(dx: 0, dy: -1300), duration: 7)
if gameStarted == false {
ball.removeAllActions()
ball.removeFromParent()
removeBalls([ball])
}
if gameStarted && spawnReady {
ball.runAction(moveDown)
if ball.parent == nil {
addChild(ball)
}
}
}
func removeBalls(balls : [SKSpriteNode]) {
for ball in balls {
ball.removeFromParent()
}
}
Upvotes: 0
Views: 202
Reputation: 48532
Where are you keeping track of each ball? Do you have a balls array?
Do this:
ball
nodes, say "ball"self.enumerateChildNodesWithName("ball", usingBlock: {
(node: SKNode!, stop: UnsafeMutablePointer <ObjCBool>) -> Void in
node.removeFromParent()
})
[self enumerateChildNodesWithName:@"ball" usingBlock:^(SKNode *node, BOOL *stop) {
[node removeFromParent];
}];`
Upvotes: 1