检测精灵的颜色是否不是另一个精灵的颜色
Detecting if a sprite's color is not the color of another sprite
所以我有这个 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
}
我会在你的 if
语句中添加一个 else
,它要么设置一个标志表明你没有点击错误的颜色,要么调用一个方法,该方法的代码在 default
的 if
语句。像这样:
在被证实之前这是错误的:
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/else
调用的 else
中那个方法。
所以我有这个 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
}
我会在你的 if
语句中添加一个 else
,它要么设置一个标志表明你没有点击错误的颜色,要么调用一个方法,该方法的代码在 default
的 if
语句。像这样:
在被证实之前这是错误的:
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/else
调用的 else
中那个方法。