Home > OS >  SpriteKit Particles — how to set state to have already fallen when scene loads?
SpriteKit Particles — how to set state to have already fallen when scene loads?

Time:06-21

I'm using SpriteKit particle emitter nodes for snow:

let snowEmitterNode = SKEmitterNode(fileNamed: "Snow.sks")
guard let snowEmitterNode = snowEmitterNode else { return }
snowEmitterNode.particleSize = CGSize(width: 4, height: 4)
snowEmitterNode.particleLifetime = 10
snowEmitterNode.particleBirthRate = 10
snowEmitterNode.xAcceleration = -gameSpeed
snowEmitterNode.particleLifetimeRange = 10
snowEmitterNode.position = CGPoint(x: (self.size.width/2), y: 800)
snowEmitterNode.zPosition = 80
addChild(snowEmitterNode)

It looks great, but it only begins once the scene loads, which means there is no snow at the start, and then it all suddenly starts to fall, which obviously looks very fake. Is it possible to somehow set the state of it to have already fallen partly on scene load to avoid this?

CodePudding user response:

It is.

Use the advanceSimulationTime(_:) method to advance the emission of particles by the number of seconds you specify.

E.g. snowEmitterNode.advanceSimulationTime(5)

See https://developer.apple.com/documentation/spritekit/skemitternode/1398027-advancesimulationtime for details

  • Related