删除现有的所有节点

时间:2015-07-12 00:04:39

标签: ios swift sprite-kit

在我的游戏中,我有一种降低精灵的方法。当用户输了(gameStarted == false)时,我需要一种方法来删除我带入场景的所有这些节点。我试图通过两种方式删除这些节点,只需说明ball.removeFromParent(),并在底部使用removeBalls函数。这两种方法都阻止了未来的球进入场景,但没有移除已经存在的球。如果有人有办法做到这一点,我真的很感激。

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()
            }
        }

1 个答案:

答案 0 :(得分:1)

你在哪里跟踪每个?你有一个阵列吗?

这样做:

  1. ball个节点命名,说“ball”
  2. 枚举
  3. 夫特

    self.enumerateChildNodesWithName("ball", usingBlock: {
        (node: SKNode!, stop: UnsafeMutablePointer <ObjCBool>) -> Void in 
        node.removeFromParent()
    })
    

    的OBJ-C

    [self enumerateChildNodesWithName:@"ball" usingBlock:^(SKNode *node, BOOL *stop) {
         [node removeFromParent];
     }];`