Particles can add incredible effects in your games. In this tutorial, see how particles can be added to a SpriteKit scene through the editor.
- [Instructor] In SpriteKit, you can add a particle system to your game by simply dragging an emitter out to the screen. Now, the emitter can be found in the object library if you scroll down a little bit. So here is this emitter and I'll click and drag it out. Now let's say what we want to do here is make it look like there's a whole bunch of stars coming down in the game so it looks like the player's actually flying through space. So I'm going to take this emitter and I'm going to put it at the top of the screen and my goal is to shoot particles downward.
So I'm going to center it by setting its X position to zero. So I'll leave it right there. And I want to point out that I have imported another asset and that's this particle and it's just a white square. Now when you're using a particle system like we're doing right now, you're going to need to have a graphic to use as your particle. You can of course use existing graphics like our item or enemy or whatever you want. You could even use a spaceship as your particle for your particle system.
Let's go back into our game scene and set up our emitter. So with it selected, I am going to start with this particle section and set its texture to our particle and then I'm going to set up the emitter's birthrate and maximum. Birthrate is how many particles per second are created. I'm going to start with 150 for now and press Enter on my keyboard. Maximum is the maximum number of particles that are created.
So if you have a number greater than zero, the particle's going to stop eventually. If we want to make stars and a character flying through space, we don't want to have any number above zero. You might make this above zero, however, if you're going to make an explosion and the particles are all going to shoot out and it's going to stop emitting after a certain point. Lifetime is how long the particles are going to be on the screen. Let's start with five seconds. I'll press five and press Return on the keyboard.
Range gives you a plus or minus value. So if I have a range of one, that'll make it go from four, that's five minus one, to six, which is five plus one. For now I'm going to leave range at zero. For position range, let's say when a particle spawns, it has a certain range. It's going to go 300 pixels either way if I put that value in here. So I'll put a 300 right here and press Return on my keyboard and then we need just a few more settings before we can see our particle in action.
So now I'm going to change the angle. That's the angle at which they move when they're moving. 90 degrees means they're going to be going upward. So what I want to do is have them going down. So I'm going to change 90 to 270. And finally what I need to do is set the speed. I'll set the start speed to 100 for now and press Return on my keyboard. The last thing I want to set is the scale because if I run this right now, they're going to be a little bit big.
So I'll hit Animate and we can see that the particles are gigantic. So I'll hit Layout and go back and then I'm going to change the scale to 0.1 and then change the range, and this is how much bigger or smaller they get, to 0.05, press Return on the keyboard and now when I animate the particles, they're nice and tiny. Notice that they're going in front of these other objects. So I'll click Layout and then I'm going to modify the Z position.
I can do that by going up a little bit and change the Z position from zero to negative one. What that's going to do is push it behind other objects on the screen. Now if I animate, we'll see that the particles are going behind all of the objects. Now if I hit the Animate button, you'll actually notice that the particles stop before they hit the bottom of the screen. That's something we can change by modifying the lifetime. So I'll hit Animate again and I'm going to drag that Lifetime slider and they should start to get destroyed toward the end.
So I'll create that a little bit larger by dragging the slider and that looks good. So now we have this whole particle system and it's working nicely. Now if you want to adjust the speed, you can do that as well. So if I hit Animate again, I can drag that Speed slider and get them moving a little bit faster. So I'll pan up a little bit. So if you want to be flying through space faster, you can modify this number and you can just do that to your liking. I think about 200 or 300 is pretty good for what we're trying to achieve here.
So now we've successfully built a particle system. So when you want to build particles, just drag out an emitter onto the screen, drop it where you like and adjust the settings through the Attributes Inspector.
- Creating a SpriteKit project
- Adding and modifying sprites
- Creating sprites in code
- Transitioning between scenes
- Working with children and parents
- Adding physics
- Detecting and responding to collisions
- Applying particle effects
- Creating actions
- Working with sound
- Adding cameras and lights