open func nodes(at p: CGPoint) -> [SKNode]May be useful for determining which nodes you touched a point. After that, you can exclude the zone CGRectfor each node that you want.
import SpriteKit
class GameScene: SKScene {
var warningZone = CGRect(x: -80, y: -60, width: 100, height: 100)
override func didMove(to view: SKView) {
let nodeRed = SKSpriteNode.init(color: .red, size: CGSize(width:300,height:200))
nodeRed.name = "nodeRed"
addChild(nodeRed)
nodeRed.position = CGPoint(x:self.frame.midX,y:self.frame.midY)
let nodeBlue = SKSpriteNode.init(color: .blue, size: CGSize(width:300,height:200))
nodeBlue.name = "nodeBlue"
addChild(nodeBlue)
nodeBlue.position = CGPoint(x:self.frame.midX+100,y:self.frame.midY+20)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let nodesTouched = self.nodes(at: location)
for node in nodesTouched {
guard let n = node.name else { return }
print("Node touched: \(node.name) at point:\(location)")
switch n {
case "nodeRed":
print("\(node.frame)")
if !warningZone.contains(location) {
print("you have touch a safe red zone")
}
case "nodeBlue":
print("\(node.frame)")
default:
break
}
}
}
}
}
Exit (I drew a warningZone with a white rectangle just to show you where it is) .:

source
share