Particle Emitter Tutorial in Sprite Kit with Swift

The particle emitter is used to create special effects to games. An image file is created to represent the particle. The emitter generates multiple instances of this particle and applies the properties using the particle emitter editor. In this tutorial a rain particle emitter will be displayed to the scene. This tutorial is built in iOS8 with Xcode 6.3.2.

Open Xcode and create a new Project. Choose iOS->Application->Game. For product name, use SpriteKitSwiftParticleTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure SpriteKit is selected as Game Technology and only iPhone is selected in Devices.

Create a New File. Choose iOS->Resource->SpriteKit Particle File.

Choose Rain as Particle Template

Enter Rain as Filename. This will create a Rain.sks file and an corresponding spark.png file. When the file is created it is opened and the SKNode editor is displayed. In this Editor the Particle Emittor properties can be edited and the changes will be applied immediately.

Here is a short description of the properties:

  • Background : used for testing for the appearance of the particle emitter in the preview
  • Particle Texture: image file containing the texture
  • Birthrate: defines the rate at which new particles are emitted
  • Lifetime: controls the length of time in seconds that a particle is visible
  • Position Range: defines the location from which particles are created.
  • Angle: the angle from which the particle will move away from the creation point.
  • Speed: initial speed at time of creation
  • Acceleration: amount of acceleration or deceleration of a particle
  • Scale: amount of growth during the lifetime of a particle
  • Rotation: amount of rotation during the lifetime of a particle

Go to the GameScene.swift file and delete the tochesBegan method since it's not needed for this project. The didMoveToView will be called when the scene is presented to the view. Change this method to

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    self.backgroundColor = UIColor.blackColor()
    var path = NSBundle.mainBundle().pathForResource("Rain", ofType: "sks")
    var rainParticle = NSKeyedUnarchiver.unarchiveObjectWithFile(path!) as! SKEmitterNode
    rainParticle.position = CGPointMake(self.size.width/2, self.size.height) = "rainParticle"
    rainParticle.targetNode = self.scene

The path variable contains the path to the particle file within the bundle. This file is decoded using the NSKeyedUnArchiver method. Next the SKEmmiterNode object is positioned at the top middle of the screen and assigned to the scene. Build and Run the project and some rain will be falling down.

You can download the source code of the SpriteKitSwiftParticleTutorial at the ioscreator repository on github.