How do I detect if an SKSpriteNode has been touched
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")
}
}
}
Solution 4
This will detect touches in Xcode 9.2 Swift 4.0
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
let touch:UITouch = touches.first!
let positionInScene = touch.location(in: self)
let touchedNode = self.atPoint(positionInScene)
if let name = touchedNode.name
{
if name == "playLbl"
{
print("playLbl Touched")
}
}
}
Solution 5
Implement touchesBegan
method that is called when a touch begins. Alternatively you can do this in touchesEnded
as well.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
let touch = touches.anyObject() as UITouch
let location = touch.locationInNode(self)
let nodes = self.nodesAtPoint(location)
for node in nodes
{
if node.name == "youNodeName"
{
// Node tapped
// Do something
break
}
}
}
James Brennan
Updated on July 08, 2022Comments
-
James Brennan almost 2 years
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)