SKEmitterNode: How to start/stop rain at random time?

82 Views Asked by At

I added rain to my game as SKEmitterNode particle and I want it to be shown at random time. What kind of function should I use?

Just in case, my code for rain:

func startGame () {

    if let rain = SKEmitterNode(fileNamed: "rain.sks") {
        rain.position = CGPoint(x: frame.width, y: frame.height)
        addChild(rain)
    }

....

1

There are 1 best solutions below

0
Maetschl On

You can use an action to remove the rain at a random time, for example:

if let rain = SKEmitterNode(fileNamed: "rain.sks") {
    rain.position = CGPoint(x: frame.width, y: frame.height)
    addChild(rain)
    let waitAction = SKAction.wait(forDuration: Double.random(in: 10.0 ... 50.0))
    let removeAction = SKAction.removeFromParent()
    rain.run(SKAction.sequence([waitAction, removeAction]))
}