我在使用swift的spritekit游戏的背景中有一个SKShapeNode var Circle = SKShapeNode(circleOfRadius: radius)
。这个圆圈是为了美观,所以没有任何东西与它相互作用。我希望Circle.strokeColor
在任何时候都持续变化。我当前的代码更改了SKShapeNode的笔画颜色属性,但它没有显示颜色更改,因为我在将其添加到背景后更改了属性。在整个游戏过程中,颜色保持不变,直到游戏结束,圆圈从背景中移除,然后重新创建。我的代码通过在每次更新函数运行时向var colorTime: CGFloat = 0.0
添加1来改变颜色,然后使用colorTime变量将圆圈颜色的RGB值与余弦函数联系起来。如何连续更改(和显示)圆圈的颜色?
override func update(currentTime: CFTimeInterval) {
if last_update_time == 0.0 {
delta = 0
} else {
delta = currentTime - last_update_time
}
last_update_time = currentTime
colorTime += 100
redColor = (cos(colorTime/100)+1)/2
greenColor = (cos(colorTime/200 - 2.09)+1)/2
blueColor = (cos(colorTime/300 - 4.18)+1)/2
circleColor = UIColor(red: redColor, green: greenColor, blue: blueColor, alpha: 1)
Circle.strokeColor = circleColor
}
我没有意识到这段代码实际上可以工作。唯一的问题是,游戏会崩溃后,游戏重启,因为我将添加SKShapeNode到背景再次没有删除它。我添加了Circle.removeFromParent()
到我的restartGame功能,现在我很好。