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

Fattie
  • 27,874
  • 70
  • 431
  • 719
gabrielpf
  • 312
  • 2
  • 12
  • If I understand you correctly, no, you can't do that. The most you can get is to manually change the [texture](https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKEmitterNode_Ref/index.html#//apple_ref/occ/instp/SKEmitterNode/particleTexture) (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 Mar 03 '16 at 13:08
  • 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 Mar 03 '16 at 13:53
  • 6
    Something you can do is have multiple particle emitters in the same location, that is how I handle my "confetti" type effects – Knight0fDragon Mar 03 '16 at 13:54
  • 2
    This is old, you can treat an SKTexture like an atlas and have the shader select parts of the texture – Knight0fDragon Jan 23 '20 at 17:30
  • @Knight0fDragon 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! – Fattie Jan 29 '20 at 12:58
  • same for you @Whirlwind - it does sound like the answer is simply NO (unless you do it in a shader) – Fattie Jan 29 '20 at 12:59
  • 1
    Is there an example of how to do this? – Joe Sep 05 '20 at 22:16
  • Why not just use multiple copies of the same emitter but with different textures?? – pua666 Oct 31 '20 at 22:26

1 Answers1

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.

pua666
  • 326
  • 1
  • 7