在swift中不断改变背景颜色



嗨,我正在完成我的游戏,但我想添加一些细节,其中一个是通过我尝试过的SKActions的颜色循环,效果很好,但它与其他所有内容重叠。

然后我在更新功能中做了这个,但它对没有任何作用

if BGRed == 1 {RedBoolIs1 = true}
        else {RedBoolIs1 = false}
        if BGGreen == 1 {GreenBoolIs1 = true}
        else {GreenBoolIs1 = false}
        if BGBlue == 1 {BlueBoolIs1 = true}
        else {BlueBoolIs1 = false}

        //red only
        if RedBoolIs1 == true && GreenBoolIs1 == false && BlueBoolIs1 == false {
            BGGreen = BGGreen + 0.01 }
        //red and green
        if RedBoolIs1 == true && GreenBoolIs1 == true && BlueBoolIs1 == false {
            BGRed = BGRed - 0.01  }
        //green only
        if RedBoolIs1 == false && GreenBoolIs1 == true && BlueBoolIs1 == false {
            BGBlue = BGBlue + 0.01  }
        //green and blue
        if RedBoolIs1 == false && GreenBoolIs1 == true && BlueBoolIs1 == true {
            BGGreen = BGGreen - 0.01  }
        //blue only
        if RedBoolIs1 == false && GreenBoolIs1 == false && BlueBoolIs1 == true {
            BGRed = BGRed + 0.01  }
        //blue and red
        if RedBoolIs1 == true && GreenBoolIs1 == false && BlueBoolIs1 == true {
            BGBlue = BGBlue - 0.01  }
        self.backgroundColor = SKColor(red: CGFloat(BGRed), green: CGFloat(BGGreen), blue: CGFloat(BGBlue), alpha: 1)

还有可能使标签在所有颜色上都可读吗?我现在的解决方案是把两个标签放在一起,第一个是白色的,45大,上面的那个是黑色的,40大,但看起来并不总是很好

感谢

首先我们初始化如下颜色变量:

var BGRed: Float = 1
var BGGreen: Float = 0
var BGBlue: Float = 0

没有发生任何事情的原因是0.1乘以10并不等于1.0。这是因为浮点数字的准确性。我自己也问了同样类型的SO问题。

要解决此问题,请删除以下比较:

if BGGreen == 1 

并替换为:

if round(BGGreen * 100) / 100.0 == 1.0

它将解决这个特定的问题,但更好的方法是开始使用十进制浮点

对于第二个问题,没有简单的答案,但像这个问题一样,SO上有多个答案。

最新更新