Reputation: 1945
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]
Upvotes: 4
Views: 2097
Reputation: 6876
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:
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 :)
Upvotes: 6