【问题标题】:Constantly changing the background color in swift不断地快速改变背景颜色
【发布时间】:2014-08-29 15:48:35
【问题描述】:

您好,我正在完成我的游戏,但我想添加一些细节,其中一个正在循环通过我尝试过 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)

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

谢谢

【问题讨论】:

    标签: colors swift sprite-kit xcode6 spectrum


    【解决方案1】:

    首先我们像这样初始化颜色变量:

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

    什么也没发生的原因是 0.1 加 10 次不等于 1.0。这是因为浮点数精度。我自己问过同类型的SO question

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

    if BGGreen == 1 
    

    并替换为:

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

    它将解决这个特殊问题,但更好的方法是开始使用decimal floating point

    对于第二个问题,没有简单的答案,但在 SO 上有多个答案,例如 one

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2015-06-16
      • 1970-01-01
      • 2021-06-27
      • 2012-04-02
      相关资源
      最近更新 更多