簡體   English   中英

不斷快速更改背景顏色

[英]Constantly changing the background color in 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)

也可以使標簽在所有顏色上可讀嗎? 我現在的解決方案是將2個標簽相互放置,第一個標簽是白色且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像這樣多個答案一個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM