在我的游戏中,我有一种降低精灵的方法。当用户失败时(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()
}
}
您在哪里跟踪每个球?你有滚珠阵列吗?
做这个:
ball
节点起个名字,说“ ball”self.enumerateChildNodesWithName("ball", usingBlock: {
(node: SKNode!, stop: UnsafeMutablePointer <ObjCBool>) -> Void in
node.removeFromParent()
})
[self enumerateChildNodesWithName:@"ball" usingBlock:^(SKNode *node, BOOL *stop) {
[node removeFromParent];
}];`
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句