GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

How to emit particles using SKEmitterNode

Swift version: 5.10

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.

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 7.0 – see Hacking with Swift tutorial 11

Similar solutions…

About the Swift Knowledge Base

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

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.7/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.