Reputation: 387
Is it possible to change how often a particle is emitted. For example, if I have one particle emitting, can I have it emit every 5 or 10 seconds?
I searched the documentation, but could not find anything. Is there a workaround? I would like to do something like this:
emitter.particleBirthRate = 1
emitter.particleBirthRateFrequency = 5 // this does not exist
Upvotes: 3
Views: 192
Reputation: 6061
I stay away from timers, there really isn't a need for them in SpriteKit
You have a built in timer function with the update func, or you can Just use actions to control your time.
what you are probably looking for is particle.resetSimulation()
I would do what you need to do like so
you can also put a key on your action and stop it by key name whenever needed
if let spark = self.childNode(withName: "sparkin") as? SKEmitterNode {
self.spark = spark
//set time here to how long in between restarting the emitter
let waiter = SKAction.wait(forDuration: 5)
let resetter = SKAction.run( { self.spark.resetSimulation() } )
let seq = SKAction.sequence([waiter, resetter])
let repeater = SKAction.repeatForever(seq)
run(repeater)
}
Upvotes: 4
Reputation: 511
I also had the same issue. Now I now its frowned upon to use Timers with spritekit but this is how I got it working.
I created the SKEmitterNode.sks within the XCODE particle editor the way wanted it and got it emitting with a timer.
var count = 0
//Create an emitter every 5 seconds
timer = Timer.scheduledTimer(withTimeInterval: 5 , repeats: true){ t in
count += 1
let emitter = SKEmitterNode(fileNamed: "emitter")
emitter?.targetNode = self
emitter?.position = self.nodeA.position
emitter?.zPosition = -5
self.addChild(emitter!)
//Remove the emitter node after 5 seconds
_ = Timer.scheduledTimer(withTimeInterval: 5, repeats: true){_ in
emitter?.removeFromParent()
}
if count == 10 {
t.invalidate()
}
}
Important thing to remember when you present a new scene, is to invalidate your timer or else your old scene will only deinit{} when count is reached.
timer.invalidate()
Upvotes: 1