3

I have come across a problem where I'm having trouble deleting several SKEmitterNodes at different times. Each SKEmitterNode is created through an iteration, so they have different times for when to be deleted (they should be deleted after they have emitted all of their specified particles). This is what I currently have:

let explosionNode = SKEmitterNode(fileNamed: "Explosion.sks")
explosionNode?.position = missile.position
explosionNode?.numParticlesToEmit = 350
addChild(explosionNode!)
let nodesEmit = CGFloat((explosionNode?.numParticlesToEmit)!) / (explosionNode?.particleBirthRate)!
let seconds = nodesEmit + (explosionNode?.particleLifetime)! + (explosionNode?.particleLifetimeRange)! / 2
run(SKAction.sequence ([
    SKAction.run(removeExplosion(node: explosionNode!)),
        SKAction.wait(forDuration: TimeInterval(seconds)),
    ])
)

and the function is

func removeExplosion(node: SKEmitterNode) {
    node.removeFromParent()
    print("REMOVED")
}

However, I'm getting an error saying Cannot convert value of type '()' to expected argument type '() -> Void'. Is there anyway to run an SKAction with parameters or is there another way to delete SKEmitterNodes after they have finished emitting all their particles?

shallowThought
  • 19,212
  • 9
  • 65
  • 112
J.Treutlein
  • 963
  • 8
  • 23
  • 3
    This is how you can do it : http://stackoverflow.com/a/31731439/3402095 (it is a Swift 2 answer though, so syntax might be a bit different for Swift 3). – Whirlwind Jan 13 '17 at 09:31
  • What is another possible way than to create an SKAction with a function and a wait duration? Sorry my Xcode is a bit laggy at the moment so I'm not able to check. – J.Treutlein Jan 13 '17 at 09:31
  • There is no other way. SKEmitterNode doesn't have a callback when it finishes with emitting. So the node stays in a node tree and eat up resources in some way. So you have to remove it manually, using actions. – Whirlwind Jan 13 '17 at 09:37
  • Thanks @WhirlWind that works perfectly! Just what I was looking for :D – J.Treutlein Jan 13 '17 at 09:42

0 Answers0