2014-08-29 30 views
0

嗨有即时通讯完成我的游戏,但我想添加一些细节,其中之一是循环槽颜色周期我已经尝试过SKActions,这很好,但它重叠了一切。不断改变背景颜色在swift中

然后我在更新功能做这个,但它不会做任何事情

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大,但它并不总是好的

感谢

回答

0

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

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

没有任何事情发生的原因是加0.1 0.1倍不等于1.0。这是因为浮点数精度。我自己问过同类型的SO question

为了解决这个问题,除去比较是这样的:

if BGGreen == 1 

,代之以:

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

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

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

+0

完美作品的魅力! – 2014-08-30 13:13:52