I have an SKShapeNode that I have created and given a CGPath to. This is in my GameScene.swift in didMoveToView:
let myNode = SKShapeNode()
let lineToDraw = CGPathCreateMutable()
CGPathMoveToPoint(lineToDraw, nil, 0, 0)
CGPathAddLineToPoint(lineToDraw, nil, 87, 120)
myNode.path = lineToDraw
myNode.strokeColor = SKColor.redColor()
myNode.lineWidth = 20
myNode.name = "My Node!"
world.addChild(myNode) // World is a higher-level node in my scene
And this is how I'm detecting touches, again in the Scene:
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
if let touch = touches.anyObject() as? UITouch {
if let shapeNode = nodeAtPoint(touch.locationInNode(self)) as? SKShapeNode {
println("Touched \(shapeNode.name)")
} else {
println("Nothing here")
}
}
}
The line node is showing up with no issues. However, it is not registering touchesEnded at all.
Really, I have three questions nebulous to this:
Is there a better way to create an SKShapeNode that is a line between two points without calling a convenience initializer? I plan on subclassing SKShapeNode to add additional logic to the node, and the subclass doesn't have access to convenience initializers of the superclass.
More importantly, how do I get the scene to recognize my line node there and trigger touchesEnded?
Is there a way, using that mechanism, I can make it a bit more "fuzzy", so it handles touches close to the line, instead of on the line? My eventual plan is to make it thinner, but I'd still like to have a large touch zone. I figure if nothing else, I can create two nodes: one clear/thick and the other red/thin, and handle touch events on the clear one, but I would like to know if there's a better way.