SpriteKit Particle Emitter Multi Image
Asked Answered
B

1

14

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

Brandie answered 3/3, 2016 at 10:16 Comment(8)
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...Blindheim
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.Brandie
Something you can do is have multiple particle emitters in the same location, that is how I handle my "confetti" type effectsHeyes
This is old, you can treat an SKTexture like an atlas and have the shader select parts of the textureHeyes
@Heyes there's a bounty waiting on this question if you pop in an answer - at worst you could just paste in your comment as an answer!Bellbottoms
same for you @Blindheim - it does sound like the answer is simply NO (unless you do it in a shader)Bellbottoms
Is there an example of how to do this?Hedron
Why not just use multiple copies of the same emitter but with different textures??Peon
P
1

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.

Peon answered 31/10, 2020 at 23:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.