Recycling emitter nodes with particle pools
We do not need to create a new emitter node for every single crate in our game. Instead, we will create a small pool of emitter nodes when the game starts and reposition them when the player smashes a crate. This is a performance best practice as emitter nodes can use system resources quickly. Follow these steps to create a particle emitter node pooling system:
In Xcode, create a new
.swift
file namedParticlePool.swift
, and add the following code to your new file:import SpriteKit class ParticlePool { var cratePool: [SKEmitterNode] = [] var heartPool: [SKEmitterNode] = [] var crateIndex = 0 var heartIndex = 0 // A property to store a reference to the GameScene: var gameScene = SKScene() init() { // Create 5 crate explosion emitter nodes: for i in 1...5 { // Create a crate emitter...