NEW: Master Swift design patterns with my latest book! >>

How to emit particles using SKEmitterNode

Written by Paul Hudson    @twostraws

SpriteKit has built-in support for particle systems, which are a realistic and fast way to create effects such as smoke, fire and snow. Even better, Xcode has a built-in visual particle editor so that you can tweak your designs until they look exactly right.

To get started, right-click on your project in Xcode and choose New File. Select iOS > Resource > SpriteKit Particle File, then choose the Smoke template and click Next to name your effect. Once that's done, your particle will be opened immediately in the visual editor so you can adjust its design.

When it comes to using your effect, just create a new SKEmitterNode object using the name of your particle effect, like this:

if let particles = SKEmitterNode(fileNamed: "yourParticleFile.sks") {
    particles.position = player.position
    addChild(particles)
}

Obviously you will want to set your own position rather than using an example player node.

Available from iOS 7.0 – see Hacking with Swift tutorial 11

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Get my latest video for free

Learn about value types, functional programming, and protocol-oriented programming in this new video – it's free!

Click here to visit the Hacking with Swift store >>