Particle Emitter iOS Tutorial

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 made with Xcode 9 and built for iOS 11.

Open Xcode and create a new Project. Choose the Game template. Choose Next

For product name, use IOS11ParticleEmitterTutorial 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. Choose Next

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

Choose Rain as Particle Template

Enter Rain as Filename. This will create a Rain.sks file and an corresponding spark file in the Assets Library. When the file is created it is opened. Open the Attribute Inspector. 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 everything inside the GameScene class except the update(_:) method. Add the didMove(to:) method.

override func didMove(to view: SKView) {
    self.backgroundColor = UIColor.black
        
    let path = Bundle.main.path(forResource: "Rain", ofType: "sks")
    let rainParticle = NSKeyedUnarchiver.unarchiveObject(withFile: path!) as! SKEmitterNode
        
    rainParticle.position = CGPoint(x: self.size.width/2, y: self.size.height)
    rainParticle.name = "rainParticle"
    rainParticle.targetNode = self.scene
        
    self.addChild(rainParticle)
}

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 IOS11ParticleEmitterTutorial at the ioscreator repository on github.