有5个颜色不同的球。 choiceBall每次点击都会随机更改纹理(颜色),并指示您必须点击其他四个彩色球中的哪个。我想做一个if语句,检查我轻击的球是否与choiceBall相同的纹理,但似乎找不到可行的方法。

在这里,我以为如果choiceBall变成红色,然后按红色球,则将打印RED。但这似乎没有发生。因为我希望每次点击球时都打印红色,蓝色,黄色或绿色,所以我不应该接触。

谢谢你的帮助。 :)

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
    choiceBall.texture = array[randomIndex]


    if choiceBall.texture == redBall.texture {
        println("RED")
    }
    else if choiceBall.texture == blueBall.texture
    {
        println("BLUE")
    }
    else if choiceBall.texture == yellowBall.texture {
        println("YELLOW")
    }
    else if choiceBall.texture == greenBall.texture {
        println("GREEN")
    }

}

最佳答案

有关您可能正在寻找的解决方案,请参见this answer on another StackOverflow post

在游戏架构方面,我建议在Color上使用choiceBall(或类似的东西)枚举,因为那样您就不会比较实际的纹理,而只是比较每个球的Color的类型。它将使代码更简洁,您也可能会从中挤出更多功能。

示例:

enum Color {
    case Red, Blue, Yellow, Green
}

[...]

if choiceBall.colorType == .Red {
    println("RED")
}
else if choiceBall.colorType == .Blue {
    println("BLUE")
}
else if choiceBall.colorType == .Yellow {
    println("YELLOW")
}
else if choiceBall.colorType == .Green {
    println("GREEN")
}

关于ios - Swift:if语句检查Node Texture是否与另一个Node Texture相同,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31505345/

10-10 21:08