我有一個菜單按鈕(skSpriteNode)和小遊戲圖標,這個精靈裏面spritekit使childnode沒有可觸摸
mainButton.addChild (playIcon)
mainButton.name = "xyz"
addchild (mainButton)
好吧,我給該按鈕的名稱,以檢查是否有此名稱的精靈被觸摸。
當我觸摸按鈕內的圖標子時,touchedNode.name爲零。我爲圖標子設置了isUserInteractionEnabled = false。我想通過觸摸到父母。我怎樣才能做到這一點。
for touch in touches {
let location = touch.location(in: self)
let touchedNode = self.atPoint(location)
if (touchedNode.name != nil) {
print ("node \(touchedNode.name!)")
} else {
continue
}
}
謝謝,我學到了一些東西。如何檢查觸摸位置是否在透明像素上?當我有兩個帶無形窗體的按鈕,但重疊的矩形區域時,單擊zposition中的第一個按鈕。 – bluelemonade
@bluelemonade這可以通過至少兩種方式解決(例如,使用CGImage提取像素阿爾法值)或製作一個不規則形狀的物理體(這實際上取決於形狀)並使用PhysicsWorld的'bodyAt(point:)'方法。看看這個:https://stackoverflow.com/a/20932809/3402095 – Whirlwind
好吧,我可以檢測到紋理的alpha。但觸摸事件並沒有向底層按鈕「冒泡」。 – bluelemonade