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 ...
Get Swift Game Development - Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.