【问题标题】:Detecting if a sprite's color is not the color of another sprite检测一个精灵的颜色是否不是另一个精灵的颜色
【发布时间】:2015-10-07 01:21:28
【问题描述】:

所以我有这个 sprite kit 游戏,它是用 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  
}

【问题讨论】:

    标签: swift colors sprite-kit swift2 skspritenode


    【解决方案1】:

    我会在你的if 语句中添加一个else,它要么设置一个标志,表明你没有点击错误的颜色,要么调用一个代码在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,看看isSafeClick是不是true

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

    对于方法方式,只需将代码直接放在该文本上方的方法中(命名为“safeScorePoint”之类的名称),然后在每个if/elseelse 中调用该方法。

    【讨论】:

    • 我不知道 switch 语句有一个 else。谢谢
    • switch 语句没有,您使用的if 语句inside case 可以有一个else。不客气!
    猜你喜欢
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多