Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading