当多次敲击时,Swift SpriteKit会更改节点的颜色



我试图根据数组中的颜色将其更改菜单上的节点的填充物。

我有一个装有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
        }
    }
}

但是,这只会读一台水龙头,我需要它注册多个水龙头

在更改颜色后触摸外的索引的初始化开始并递增索引。

var colors = [UIColor.blue, UIColor.red, UIColor.green, UIColor.purple]
var index = 0
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first!
    if changeColor.contains(touch.location(in: self)) {
        changeColor.fillColor = colors[index]
        index += 1
        if index == colors.count {
            index = 0
        }
    }
}

最新更新