sprite kit - spritekit make childnode not touchable -
i have menu button (skspritenode) , small play icon inside sprite
mainbutton.addchild (playicon) mainbutton.name = "xyz" addchild (mainbutton) ok, gave button name check if sprite name touched.
when touch icon child inside button, touchednode.name nil. set isuserinteractionenabled = false icon child. want pass touch parent. how can this.
touch in touches { let location = touch.location(in: self) let touchednode = self.atpoint(location) if (touchednode.name != nil) { print ("node \(touchednode.name!)") } else { continue } }
you have implement touchesbegan in subclass of skspritenode , set isuserinteractionenabled = true, in order accept , process touches particular node (button).
import spritekit protocol buttondelegate:class { func printbuttonsname(name:string?) } class button : skspritenode{ weak var delegate:buttondelegate? init(name:string){ super.init(texture: nil, color: .purple, size: cgsize(width: 250, height: 250)) self.name = name self.isuserinteractionenabled = true let icon = skspritenode(color: .white, size: cgsize(width:100, height:100)) addchild(icon) } required init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } override func touchesbegan(_ touches: set<uitouch>, event: uievent?) { delegate?.printbuttonsname(name: self.name) } } class gamescene: skscene, buttondelegate { override func didmove(to view: skview) { let button = button(name:"xyz") button.delegate = self button.position = cgpoint(x: frame.midx - 50.0, y: frame.midy) addchild(button) } func printbuttonsname(name: string?) { if let buttonname = name { print("pressed button : \(buttonname) ") } } } now, button swallow touches , icon sprite ignored.
i modified code one of answers make example specific needs, can read, in link, whole thing more in detail if interested.
Comments
Post a Comment