4

I have created a particle effect using the editor and now I'd like to change particleColor in code. I have set particleColorSequence to nil (otherwise I the colors would come from the color ramp in the editor and not my code) and particleColorBlendFactor is set to 1.0. I assign a random color to particleColor in the update method with the hopes that it will change each time through the loop. It does choose a random color the first time through, but then the color never varies. Can someone please explain why?

Global

let emitter = SKEmitterNode(fileNamed: "squares.sks")
let colors = [SKColor.red, SKColor.green, SKColor.blue]

didMove(to view:)

emitter?.particleColorBlendFactor = 1.0
emitter?.particleColorSequence = nil
addChild(emitter!)

update(_ currentTime:)

let random = Int(arc4random_uniform(UInt32(self.colors.count)))
emitter?.particleColor = colors[random]
squarehippo10
  • 1,855
  • 1
  • 15
  • 45

1 Answers1

6

This hardly counts as an answer but I couldn't fit it all into a comment so...please bear with me.

The good news is that your code seems to work!

I tried creating a new Sprite Kit project and pasted your code into that so I ended up with a GameScene class of type SKScene looking like this:

import SpriteKit
import GameplayKit

class GameScene: SKScene {
 
    let emitter = SKEmitterNode(fileNamed: "squares.sks")
    let colors = [SKColor.red, SKColor.green, SKColor.blue]
    var lastUpdateTime: TimeInterval?

    override func didMove(to view: SKView) {
        emitter?.particleColorBlendFactor = 1.0
        emitter?.particleColorSequence = nil
        addChild(emitter!)
    }

    override func update(_ currentTime: TimeInterval) {
        var delta = TimeInterval()
        if let last = lastUpdateTime {
            delta = currentTime - last
        } else {
            delta = currentTime
        }
        if delta > 1.0 {
            lastUpdateTime = currentTime
            let random = Int(arc4random_uniform(UInt32(self.colors.count)))
            emitter?.particleColor = colors[random]
        }
    }
}

And then I created a new SKEmitterNode from the template (I used fire...just to chose something) and named it squares.sks.

When I run that, I can see this:

enter image description here

So...where does that leave us?

I'm thinking there must be something different in your setup. If you try to create a new example project like mine, are you able to get it to work?

Yeah...hardly an answer I know, but think of it as a reassurance that you are on the right path at least :)

Community
  • 1
  • 1
pbodsk
  • 6,787
  • 3
  • 21
  • 51
  • I gave you credit because, you are right, there's nothing wrong with my code. There is, however, something very wrong with the particle editor. I created a new fire emitter like you did above and it worked! So I started making changes in the editor one by one until my fire emitter was exactly like my original emitter. They are now identical, setting for setting, every decimal point and negative sign included. But my original still doesn't change color while what was once the fire emitter works perfectly. Ridiculousness! – squarehippo10 Apr 18 '17 at 23:57
  • Thank you kindly for the credit. Glad you got it working in the end. I'm wondering however, what the difference between you original emitter and the new one could be. Here are some questions for you to consider (by which I mean: you don't have to answer them here if you don't feel like it :)) Did you create the original in code or from a template file? Was it created in a different version of Xcode? – pbodsk Apr 19 '17 at 06:21
  • If you're really desperate you could try to open the two .sks files in a text editor and see if you can spot any difference there...or try the diff command in a terminal (http://stackoverflow.com/questions/12118403/comparing-binary-files). Good luck onwards :) – pbodsk Apr 19 '17 at 06:22
  • So, in trying to get to the bottom of things I switched the new emitter for the old one today and...it worked! Which is crazy, because I haven't changed the original file at all. (I created it in the particle editor a few days ago.) I've experienced a fair bit of flakiness with this version of Xcode. Maybe I just need to reinstall it. – squarehippo10 Apr 19 '17 at 19:29
  • Haha...computers eh ¯\_(ツ)_/¯ Glad you got to the bottom of the mystery and sorry that there was no logical explanation to it...but...continue to the next problem right :) – pbodsk Apr 19 '17 at 20:38