在 spritekit 中更改节点颜色
Change node color in spritekit
我有一个树枝的矢量插图,上面有几片叶子。我正在将分支从屏幕底部移动到屏幕顶部。但是当分支从初始位置(屏幕底部)移动到最终位置(屏幕顶部)时,我希望分支在移动到顶部时将颜色从绿色变为橙色。它应该是一个平稳的过渡。我有树枝和树叶作为矢量图像。我正在使用 spritekit(ios) 进行开发。该分支已附加在下面的 link 中
我不希望颜色立即从绿色变为红色。我希望它随着分支移动到顶部而从绿色慢慢过渡到红色
图片
你需要 2 张图片,绿色的一张
还有红色的。
元素
现在你需要一个节点来保存两个图像
class Element: SKNode {
private let green = SKSpriteNode(imageNamed: "green")
private let red = SKSpriteNode(imageNamed: "red")
override init() {
super.init()
self.addChild(green)
red.alpha = 0
self.addChild(red)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func turnRed(duration: TimeInterval) {
green.run(.fadeOut(withDuration: duration))
red.run(.fadeIn(withDuration: duration))
}
}
动画
最后你只需要调用turnRed(duration:)
方法
class GameScene: SKScene {
let element = Element()
override func didMove(to view: SKView) {
self.addChild(element)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// move the element to bottom
let bottomY = self.frame.minY + element.calculateAccumulatedFrame().height / 2
let moveToBottom = SKAction.moveTo(y: bottomY, duration: 2)
element.run(moveToBottom)
// change the color
element.turnRed(duration: 2)
}
}
更新
如果您想将颜色设置为给定的绿色和红色比例,请使用以下代码
class Element: SKNode {
private let green = SKSpriteNode(imageNamed: "green")
private let red = SKSpriteNode(imageNamed: "red")
override init() {
super.init()
self.addChild(green)
red.alpha = 0
self.addChild(red)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// colorProgressionRatio must be a value between 1 (full green) and 0 (full red)
func update(colorProgressionRatio: CGFloat) {
guard colorProgressionRatio >= 0 && colorProgressionRatio <= 1 else {
debugPrint("colorProgressionRatio must be a value between 0 and 1")
return
}
let greenIntensity: CGFloat
let redIntensity: CGFloat
if colorProgressionRatio == 0 {
greenIntensity = 0
redIntensity = 1
} else if colorProgressionRatio == 1 {
greenIntensity = 1
redIntensity = 0
} else {
greenIntensity = colorProgressionRatio
redIntensity = 1 - colorProgressionRatio
}
green.run(.fadeAlpha(to: greenIntensity, duration: 2))
red.run(.fadeAlpha(to: redIntensity, duration: 2))
}
}
现在您只需拨打
element.update(colorProgressionRatio: 0.4)
传递一个介于 0 和 1 之间的值(0 表示红色,1 表示全绿色)。
我有一个树枝的矢量插图,上面有几片叶子。我正在将分支从屏幕底部移动到屏幕顶部。但是当分支从初始位置(屏幕底部)移动到最终位置(屏幕顶部)时,我希望分支在移动到顶部时将颜色从绿色变为橙色。它应该是一个平稳的过渡。我有树枝和树叶作为矢量图像。我正在使用 spritekit(ios) 进行开发。该分支已附加在下面的 link 中 我不希望颜色立即从绿色变为红色。我希望它随着分支移动到顶部而从绿色慢慢过渡到红色
图片
你需要 2 张图片,绿色的一张
还有红色的。
元素
现在你需要一个节点来保存两个图像
class Element: SKNode {
private let green = SKSpriteNode(imageNamed: "green")
private let red = SKSpriteNode(imageNamed: "red")
override init() {
super.init()
self.addChild(green)
red.alpha = 0
self.addChild(red)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func turnRed(duration: TimeInterval) {
green.run(.fadeOut(withDuration: duration))
red.run(.fadeIn(withDuration: duration))
}
}
动画
最后你只需要调用turnRed(duration:)
方法
class GameScene: SKScene {
let element = Element()
override func didMove(to view: SKView) {
self.addChild(element)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// move the element to bottom
let bottomY = self.frame.minY + element.calculateAccumulatedFrame().height / 2
let moveToBottom = SKAction.moveTo(y: bottomY, duration: 2)
element.run(moveToBottom)
// change the color
element.turnRed(duration: 2)
}
}
更新
如果您想将颜色设置为给定的绿色和红色比例,请使用以下代码
class Element: SKNode {
private let green = SKSpriteNode(imageNamed: "green")
private let red = SKSpriteNode(imageNamed: "red")
override init() {
super.init()
self.addChild(green)
red.alpha = 0
self.addChild(red)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// colorProgressionRatio must be a value between 1 (full green) and 0 (full red)
func update(colorProgressionRatio: CGFloat) {
guard colorProgressionRatio >= 0 && colorProgressionRatio <= 1 else {
debugPrint("colorProgressionRatio must be a value between 0 and 1")
return
}
let greenIntensity: CGFloat
let redIntensity: CGFloat
if colorProgressionRatio == 0 {
greenIntensity = 0
redIntensity = 1
} else if colorProgressionRatio == 1 {
greenIntensity = 1
redIntensity = 0
} else {
greenIntensity = colorProgressionRatio
redIntensity = 1 - colorProgressionRatio
}
green.run(.fadeAlpha(to: greenIntensity, duration: 2))
red.run(.fadeAlpha(to: redIntensity, duration: 2))
}
}
现在您只需拨打
element.update(colorProgressionRatio: 0.4)
传递一个介于 0 和 1 之间的值(0 表示红色,1 表示全绿色)。