Home How to use SKEmitterNode programatically
Post
Cancel

How to use SKEmitterNode programatically

Lately, I was developing a game for the AppleTV and WatchOS, and while doing that, I was looking for a way to create the SKEmitterNode programmatically. Surprisingly that was hard! The main reason for that was the lack of resources and tutorials, so I thought I’ll create that!.

What is SKEmitterNode?

SKEmitterNode is a node that automatically creates and renders small partical sprites.

How to create SKEmitterNode

This is the code a wrote to create the explostion in my game, below that i’ll explain what each and every line do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    private func addExplosion() {
    let size = size.width * 0.01
    let emitter = SKEmitterNode()
    emitter.particleSize = CGSize(width: size, height: size)
    emitter.particleZPosition = Layer.logoExplosion.rawValue
    emitter.numParticlesToEmit = 100
    emitter.particleBirthRate = 300
    emitter.particleLifetimeRange = 5
    emitter.emissionAngleRange = 360 * .pi / 180
    emitter.particleSpeed = 700
    emitter.particleColor = .white
    emitter.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
    emitter.particleColorSequence = SKKeyframeSequence(keyframeValues: [SKColor.yellow,
                                                                        SKColor.red,
                                                                        SKColor.lightGray,
                                                                        SKColor.gray],
                                                       times: [0, 0.25, 0.5, 1])
    addChild(emitter)
  }

particleSize

Particle size is the size of every single particle. Make sure it give it enough size to be visible.

particleZPosition

Particle Z position is the postion for the particle, so to make it above another node the z postion should be higher than that node. If you have a background with z postion 1 the particle z postion should be more than 1 to be visiable.

numParticlesToEmit

The number of particles to emit, if it’s is 100 it will emit 100 particle. The default is 0 which means it will keep emiting until you remove it from the scene.

particleBirthRate

The number of particles that will be generated by the emitter every second.

particleLifetimeRange

The range of life time for each particle. if you give 2 the range will be between 1 and 3.

emissionAngleRange

The emission angle range for the emitter. if you give it 360. the random values will be 360 (-/+) 180.

particleSpeed

The speed for each particle in points per second. so 700 means 700 point per second.

particleColor

The color for each particle.

position

The postion for the emitter.

particleColorSequence

The color sequence for each particle, so in my case it will start with yallow, red, light gray, then gray.

addChild

To make the emitter start emitting you need to do a one thing. Add it to the scene, once you add it, it will directly start emitting.

I hope that this helped someone to create there awesome game!

-❤️~.
If you have any questions you can send me a message on Twitter or facebook. Also you can check my Github page or my Apps.

This post is licensed under CC BY 4.0 by the author.