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

Special effects: SKEmitterNode

Our current destroy() method does nothing much, it just removes the ball from the game. But I made it a method for a reason, and that's so that we can add some special effects now, in one place, so that however a ball gets destroyed the same special effects are used.

Perhaps unsurprisingly, it's remarkably easy to create special effects with SpriteKit. In fact, it has a built-in particle editor to help you create effects like fire, snow, rain and smoke almost entirely through a graphical editor. I already created an example particle effect for you (which you can customize soon, don't worry!) so let's take a look at the code first.

Modify your destroy() method to this:

func destroy(ball: SKNode) {
    if let fireParticles = SKEmitterNode(fileNamed: "FireParticles") {
        fireParticles.position = ball.position
        addChild(fireParticles)
    }

    ball.removeFromParent()
}

The SKEmitterNode class is new and powerful: it's designed to create high-performance particle effects in SpriteKit games, and all you need to do is provide it with the filename of the particles you designed and it will do the rest. Once we have an SKEmitterNode object to work with, we position it where the ball was then use addChild() to add it to the scene.

If you run the app now, you'll see the balls explode in a fireball when they touch a slot – a pretty darn amazing effect given how little code was written!

But the real fun is yet to come, because the code for this project is now all done and you get to play with the particle editor. In Xcode, look in the Content folder you dragged in and select the FireParticles.sks file to load the particle editor.

The particle editor is split in two: the center area shows how the current particle effect looks, and the right pane shows one of three inspectors. Of those three inspectors, only the third is useful because that's where you'll see all the options you can use to change the way your particles look.

At the time of writing, Xcode's particle editor is a little buggy, so I suggest you change the Maximum value to 0 before beginning otherwise you might see nothing at all.

Confused by all the options? Here's what they do:

  • Particle Texture: what image to use for your particles.
  • Particles Birthrate: how fast to create new particles.
  • Particles Maximum: the maximum number of particles this emitter should create before finishing.
  • Lifetime Start: the basic value for how many seconds each particle should live for.
  • Lifetime Range: how much, plus or minus, to vary lifetime.
  • Position Range X/Y: how much to vary the creation position of particles from the emitter node's position.
  • Angle Start: which angle you want to fire particles, in degrees, where 0 is to the right and 90 is straight up.
  • Angle Range: how many degrees to randomly vary particle angle.
  • Speed Start: how fast each particle should move in its direction.
  • Speed Range: how much to randomly vary particle speed.
  • Acceleration X/Y: how much to affect particle speed over time. This can be used to simulate gravity or wind.
  • Alpha Start: how transparent particles are when created.
  • Alpha Range: how much to randomly vary particle transparency.
  • Alpha Speed: how much to change particle transparency over time. A negative value means "fade out."
  • Scale Start / Range / Speed: how big particles should be when created, how much to vary it, and how much it should change over time. A negative value means "shrink slowly."
  • Rotation Start / Range / Speed: what Z rotation particles should have, how much to vary it, and how much they should spin over time.
  • Color Blend Factor / Range / Speed: how much to color each particle, how much to vary it, and how much it should change over time.

Note: Once you've finished editing your particles, make sure you put a maximum value back on them otherwise they'll never go away!

It's worth adding that you can create particles from one of Xcode's built-in particle template. Add a new file, but this time choose "Resource" under the iOS heading, then choose "SpriteKit Particle File" to see the list of options.

Hacking with Swift+

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and more!

Learn more here

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: 4.8/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.