I created a SKEmitterNode and I want the particleTexture property to animate images.
This is what I'm trying, but it is only showing the first image:
let animationImages = [UIImage(named: "image1")!, UIImage(named: "image2")!]
let imageView = UIImageView()
imageView.image = UIImage.animatedImage(with: animationImages, duration: 1)
imageView.startAnimating()
emitter.particleTexture = SKTexture(image: imageView.image!)
Your last line will always return the same "main" image, regardless of the animation. This was confirmed elsewhere on this site already, but see also Apple's documentation of
UIImageView.image.But even if you did manage to obtain the current image, it would still be a one-time assignment, and the texture wouldn't update automatically; that's something you will need to do yourself; as Apple puts it in Animating a Sprite by Changing its Texture in Apple's
SpriteKitreference:Supposed solution: particle action (doesn't seem to work since iOS 9)
Luckily, there's an
SKActionto do just that (see the last link for some details), and it can theoretically even be applied toSKEmitterNodeparticles (something I just found out as I was about to tell you the opposite):EDIT: It doesn't work, and hasn't worked for years:
particleActions are simply being ignored. It's quite incredible that Apple wouldn't fix such an obvious bug for half a decade, but this seems to be the case.Workaround: implement your own emitter
SKEmitterNodes have a lot going for them (this bug notwithstanding):SKSpriteNode-like thingies that are optimized for being thrown around in huge numbers without all the overhead associated with full-blown sprite nodes.However, I already found myself in a situation once where I was forced to implement my own "emitter." In my case, it involved little more than writing a method creating a fixed number of sprite nodes, and attaching a complex action (three levels of hierarchy of multiple groups and sequences) to each of them that controlled their movement, scale, alpha, and eventual disappearance (removal from parent). It was not a huge amount of work, and it gave me the precise control I needed over the particle animations that an emitter node couldn't provide – but I was lucky in that I only needed to emit a few dozen particles at a time.
I'd say implementing your own emitter would make the most sense if all of the following conditions are true:
SKEmitterNodedoesn't provide; andSKEmitterNodein Xcode, and re-use the parameters of your prototype in your final implementation; andSKEmitterNode, which may not be a trivial task.Once again, good luck:
SpriteKitcould be a fantastic framework if only Apple got around to fixing some of its pretty outrageous bugs. (I'm having issues with custom shaders… Maybe you will see a question or two from me in the near future.)