Particle Emitter iOS Tutorial

The particle emitter is used to create special effects to. An SpriteKit Particle 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 10 and built for iOS 12.

Project Setup

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

For product name, use IOSParticleEmitterTutorial 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 SpriteKit Particle File

Create a New File. Choose iOS -> Resource -> 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

Game Template Cleanup

Go to the GameScene.sks file and delete the hello World label. Select the Scene and in the Attributes inspector change the X and Y values of the Anchor point to 0.

anchor-point.png

Code Implementation

Go to the GameScene.swift file and delete everything inside the GameScene class. Add the didMove(to:) method.

override func didMove(to view: SKView) {
    backgroundColor = UIColor.black
        
    if let rainParticles = SKEmitterNode(fileNamed: "Rain.sks") {
        rainParticles.position = CGPoint(x: size.width/2, y: size.height)
        rainParticles.name = "rainParticle"
        rainParticles.targetNode = scene

        addChild(rainParticles)
    }
}

The Particle file is loaded into a SKEmmiterNode object. The particles are positioned at the top middle of the screen and assigned to the scene.

Run Project

Build and Run the project and some rain will be falling down.

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

Related Articles