14
votes

I'm trying to use SKSprite Particle Emitter with Swift.

But I want use a number of different textures in my emitter.

Is it possible to: have many images, and, have the emitter use the images randomly, instead of using only one image?

Thanks

1
If I understand you correctly, no, you can't do that. The most you can get is to manually change the texture (particleTexture) using SKAction for example, but by doing that, the new texture will be applied to all of the existing particles. And I guess that is not what you want...Whirlwind
Yes i have tried to change manually texture and of course it's applied in all particles. But thank you for confirming it's not possible. I will try do this manually so.gabrielpf
Something you can do is have multiple particle emitters in the same location, that is how I handle my "confetti" type effectsKnight0fDragon
This is old, you can treat an SKTexture like an atlas and have the shader select parts of the textureKnight0fDragon
Is there an example of how to do this?Joe

1 Answers

1
votes

Suppose you designed your emitter with one texture and saved it as "original.sks", and you have an array with the textures called textures:

var emitters:[SKEmitterNode] = []
for t in textures {
    let emitter = SKEmitterNode(fileNamed: "original.sks")!
    emitter.particleTexture = t
    emitter.numParticlesToEmit /= CGFloat(emitters.count)
    emitter.particleBirthRate /= CGFloat(emitters.count)
    emitters.append(emitter)
}

Now you have an array of emitters instead of a single one. Whatever you'd do with your emitter, just do it with the array:

// What you'd do with a single emitter:
addChild(someNormalEmitter)
someNormalEmitter.run(someAction)
...
    

// How to do the same with the array:
emitters.forEach {
    self.addChild($0)
    $0.run(someAction)
...
}

Of course, you can also subclass SKEmitterNode so that it contains other SKEmitterNode children and propagates all the usual emitter methods and actions and properties to the children… depending on your needs.