2015-10-07 108 views
0

所以我有这个精灵套件游戏,这是在swift 2编码。游戏包括这些彩色圆圈(绿色,红色,紫色,黄色,蓝色)倒下屏幕从相同的高度开始,但从不同的宽度开始。在屏幕的底部有一个栏,告诉你什么颜色不按。所以如果酒吧是黄色的,并且你点击一个黄色的圆圈,你会失去。我已经有失败的实现,但我似乎无法弄清楚如何检测单击的圆形不是栏上的颜色。这是我对颜色检测的代码。请记住,变量“colorNeeded”是你不想点击检测精灵的颜色是不是另一个精灵的颜色

switch colorNeeded { 
    case SKColor.redColor(): 
     if Red.containsPoint(location) { 
      print("Color Needed is Blue, Blue Circle Clicked") 
      print("Lose, score is: \(score)") 
      changeColorNeeded() 
     } 
     break 

    case SKColor.blueColor(): 
     if Blue.containsPoint(location) { 
      print("Color Needed is Blue, Blue Circle Clicked") 
      print("Lose, score is: \(score)") 
      changeColorNeeded() 
     } 
     break 

    case SKColor.yellowColor(): 
     if Yellow.containsPoint(location) { 
      print("Color Needed is Blue, Blue Circle Clicked") 
      print("Lose, score is: \(score)") 
      changeColorNeeded() 
     } 
     break 

    case SKColor.greenColor(): 
     if Green.containsPoint(location) { 
      print("Color Needed is Blue, Blue Circle Clicked") 
      print("Lose, score is: \(score)") 
      changeColorNeeded() 
     } 
     break 

    case SKColor.purpleColor(): 
     if Purple.containsPoint(location) { 
      print("Color Needed is Blue, Blue Circle Clicked") 
      print("Lose, score is: \(score)") 
      changeColorNeeded() 
     } 
     break 

    default: 
     if Purple.containsPoint(location) || Green.containsPoint(location) || Yellow.containsPoint(location) || Blue.containsPoint(location) || Red.containsPoint(location){ 
      score++ 
      ("Good Color Clicked") 
      ChangeCounter++ 
      if ChangeCounter == 5 { 
       changeColorNeeded() 
      } 
     } 
     break 
} 

回答

0

我想补充一个elseif陈述,要么设置一个标志,表明你没有点击错了颜色的颜色或者调用具有defaultif语句中的代码的方法。事情是这样的:

这是假的,直到证明阳性:

var isSafeClick = false 

case SKColor.redColor(): 

    if Red.containsPoint(location) { 

     print("Color Needed is Blue, Blue Circle Clicked") 
     print("Lose, score is: \(score)") 
     changeColorNeeded() 
    } else { 
     isSafeClick = true 
    } 

    break 

等,为在交换机的每个if声明。在switch语句的结束,加上在另一个if,看是否isSafeClicktrue

if (isSafeClick) { 
    score++ 
    ("Good Color Clicked") 
    ChangeCounter++ 
    if ChangeCounter == 5{ 
     changeColorNeeded() 
    } 
} 

对于方法来说,只要把代码中直接本文入一个方法(名称类似于“safeScorePoint”或者其他),并在每个if/elseelse中调用该方法。

+0

我不知道switch语句有别的。谢谢 – jacobsan420

+0

switch语句没有,'if'语句在_inside_ case中可以有'else'。别客气! – Gliderman

相关问题