I have a SKNode with userInteractionEnabled=true and touchesEnded overridden.
Since touchesEnded will be called at any time even when i move my finger out of the SKNode i want to be sure the touch ends within the SKNode, only then i want to count this as valid tap.
So i did the following:
class Tile : SKNode {
var level:Int!
init(tileWidth: CGFloat, level: Int) {
super.init()
self.name = "lvlseltile"
self.level = Int(level)
self.userInteractionEnabled = true
let node = SKShapeNode(rect: CGRect(x: 0, y: 0, width: tileWidth, height: tileWidth))
node.fillColor = UIColor.grayColor()
self.addChild(node)
let textNode = SKLabelNode(text: String(level))
textNode.fontSize = 24
textNode.fontColor = UIColor.whiteColor()
textNode.horizontalAlignmentMode = .Center
textNode.verticalAlignmentMode = .Center
textNode.position = CGPoint(x: tileWidth/2, y: tileWidth/2)
self.addChild(textNode)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let location = touches.first?.locationInNode(self)
if self.containsPoint(location!) {
print("tapped tile")
}
}
}
Since im checking the location IN the current node, i get the relative coordinates from the current SKNodes coordinate system.
But self.containsPoint always returns FALSE. My SKNode is 40x40 big, my touch is at like 20x20, so in the center of the node, but it still returns FALSE.
Why that?
I know i can do a workaround like checking the touch location if x>0 && x<=40 and y>= and y<=40. But this is what i actually expect from self.containsPoint to do. What am i doing wrong?
SKNode
? – Luca AngelettiSKNode
and that's correct. What I don't understand is why you do receive touch events on your SKNode... – Luca Angeletti