How do I detect if an SKSpriteNode has been touched
I am trying to detect if my sprite node has been touched and I have no idea where to start.
let Pineapple = SKSpriteNode(imageNamed: "Pineappleimg")
Pineapple.userInteractionEnabled = true
Pineapple.position = CGPoint(x: CGRectGetMidX(self.frame) - 200, y: CGRectGetMidY(self.frame));
self.addChild(Pineapple)
Solution 1:
First set the name
property of the SKSpriteNode
to a string.
pineapple.name = "pineapple"
pineapple.userInteractionEnabled = false
then in touchesBegan
function in the Scene
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch:UITouch = touches.anyObject()! as UITouch
let positionInScene = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name
{
if name == "pineapple"
{
print("Touched")
}
}
}
This is one way to do it.
You can also subclass SKSpriteNode
and override the touchesBegan
inside it.
class TouchableSpriteNode : SKSpriteNode
{
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
print("touched")
}
}
Then do
let pineapple = TouchableSpriteNode(imageNamed: "Pineappleimg")
pineapple.userInteractionEnabled = true
pineapple.position = CGPoint(x: CGRectGetMidX(self.frame) - 200, y: CGRectGetMidY(self.frame));
self.addChild(pineapple)
Solution 2:
If you are looking only for a few nodes that can be touched (e.g., the "Continue" or "Exit" labels in a game UI), this might be an alternative but very simple solution:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
if myNode.containsPoint(touch.locationInNode(self)) {
print("touched")
}
}
Solution 3:
Update for Swift Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1) and XCode Version 8.2.1 (8C1002):
Value of type 'Set' has no member 'anyObject'
'locationInNode' has been renamed to 'location(in:)'
'nodeAtPoint' has been renamed to 'atPoint(_:)'
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let node : SKNode = self.atPoint(location)
if node.name == "myNodeName" {
print("Hello")
}
}
}