问题
Still getting my bearings with Swift, be gentle ;-)
I have set up an action to change the color of a node, which I call every few seconds. It works like a charm on the first call, but after that the color remains the same. My code:
let duration: TimeInterval = 3
var myRed = 255
var myGreen = 0
var myBlue = 0
var voltage: UInt32 = 0 {
willSet(newV) { }
didSet {
myGreen = Int.random(in: 0...255)
let act1 = SCNAction.customAction(duration: duration, action: { (node, elapsedTime) in
let percentage = elapsedTime / CGFloat(self.duration)
self.backgroundNode.geometry?.firstMaterial?.diffuse.contents = self.aniColor(from: self.oldColor, to: self.newColor, percentage: percentage)
})
if backgroundNode.action(forKey: "color") == nil {
print("Running action")
oldColor = newColor
newColor =
UIColor(red: CGFloat(myRed), green: CGFloat(myGreen), blue: CGFloat(myBlue), alpha: 1.0)
backgroundNode.runAction(act1, forKey: "color")
}
}
print("new color: \(newColor)")
print("old color: \(oldColor)")
}
}
Printing the RBG values from the aniColor function shows that the values do change, so I'm stumped as to why it only works once. Alternative solutions for having the node's color fade into another color would also be welcome.
Thanks for your time.
EDIT: Apparently I'm not allowed to make a comment to my own question. Sigh. But the answer is in the comments, thanks E.Coms.
来源:https://stackoverflow.com/questions/54934109/scnaction-to-change-color-of-node-works-once-then-never-again