【发布时间】:2017-06-08 19:50:13
【问题描述】:
我正在尝试让它根据数组中的颜色更改菜单场景上节点的填充颜色。
我有一个填充了 4 种颜色的数组,当我按下节点时,它应该根据位置过滤这些颜色。因此,当我点击节点一次它应该将颜色变为红色时,如果再次点击它会变为绿色,再次点击会将其变为紫色,然后再变为蓝色
var colors = [UIColor.blue, UIColor.red, UIColor.green, UIColor.purple]
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
var index = 0
if changeColor.contains(touch.location(in: self)) {
index += 1
changeColor.fillColor = colors[index]
if index == 3 {
index = 0
}
}
}
但是,这只读取一次点击,我需要它注册多次点击
【问题讨论】:
-
我认为它在读取水龙头,但是每次运行此函数时您都会将索引重置为 0,因此您只会看到数组中的第一种颜色。将 var index = 0 移到函数之外应该这样做
-
更改颜色后也移动索引 += 1 否则您将永远看不到颜色 ins colors[0]
-
每次输入 touchesBegan() 时都会重置索引。将其移出范围,以便在调用之间持续存在。
标签: swift sprite-kit touchesbegan