Ios 为什么Xcode TouchesEnded()最上面的被触摸节点总是返回子节点而不是父节点?
我将SKSpriteNode子类化为MenuButton() MenuButton()包含两个子项:Ios 为什么Xcode TouchesEnded()最上面的被触摸节点总是返回子节点而不是父节点?,ios,swift,xcode,skspritenode,sknode,Ios,Swift,Xcode,Skspritenode,Sknode,我将SKSpriteNode子类化为MenuButton() MenuButton()包含两个子项: SKSpriteNode(图像) SKSpriteNode(一些文本) 运行以下命令时,如果用户触摸包含图像或文本的部分按钮(MenuButton()的子项),则不会执行该按钮,因为图像/文本与按钮具有不同的名称,并且它们是第一个touchedNode override func touchesEnded(_ touches: Set<UITouch>, with event:
(图像)SKSpriteNode
(一些文本)SKSpriteNode
touchedNode
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let touchedNode = atPoint(location)
let touchedNodeName = atPoint(location).name
if let nodeName = touchedNodeName, nodeName == ("btnPlay") { self.showGameModeMenu() }
}
但是,当另一个节点(btnCloseLeadboard
)显示在顶部的btnPlay
)上时,它将循环并执行这两个节点,而不仅仅是最顶部的节点(btnCloseLeadboard
)
我已尝试为子节点设置isUserInteractionEnabled=false
,但这不起作用。它仍然将其视为最顶层的节点
如何防止这种情况发生?如果孩子的父母的名字是“btnPlay”,或者如果名为“btnPlay”的节点本身被点击(对于图像或文本子节点之外的点击),您似乎想显示游戏菜单。如果是,您可以将顶层代码更改为:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let touchedNode = atPoint(location)
if touchedNode.parent?.name == "btnPlay" || touchedNode.name == "btnPlay" { showGameModeMenu() }
}
override func touchesend(touch:Set,带有事件:UIEvent?){
guard let touch=touch.first else{return}
让位置=触摸。位置(in:self)
让touchedNode=atPoint(位置)
如果touchedNode.parent?.name==“btnPlay”| | touchedNode.name==“btnPlay”{showGameModeMenu()}
}
btnPlay从哪里来?显示设置“btnPlay”的代码行。您可能需要使用两个不同的类进行重写,以区分顶部和底部
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let touchedNode = atPoint(location)
if touchedNode.parent?.name == "btnPlay" || touchedNode.name == "btnPlay" { showGameModeMenu() }
}