Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit Particle Emitter Multi Image

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

like image 521
gabrielpf Avatar asked Mar 03 '16 10:03

gabrielpf


1 Answers

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.

like image 121
pua666 Avatar answered Oct 10 '22 00:10

pua666