Detect tap on ARKit SKNode

I'm attempting to replicate the "blasting mode" behavior from the demo in WWDC 2017 Session 609 - Going Beyond 2D with SpriteKit.

In this demo: a tap on the screen displays an anchored emoji and in blasting mode, a tap on the emoji removes it.

I'm not sure how to detect taps on the emoji.

I've tried a few things in touchesBegan of my SKScene subclasss like:

        if let touchLocation = touches.first?.location(in: sceneView),
            let hit = sceneView.hitTest(touchLocation, types: .featurePoint).first {
            let anchor = ARAnchor(transform: hit.worldTransform)
            let node = sceneView.node(for: anchor)
            node?.removeFromParent()
        }


or


        if let touchLocation = touches.first?.location(in: sceneView),
            let node = nodes(at: touchLocation).first {
            node.removeFromParent()
        }

Accepted Reply

Ah, figured it out.


First, hitTest is not the right method to determine a tap on a sprit. This is from the docs:


> this method searches for AR anchors and real-world objects detected by the AR session, not SpriteKit content displayed in the view. To search for SpriteKit objects, use the

nodes(at:)
method of the view's SpriteKit scene.


Second, in my original code, I was converting the touch to the view's coordinate system, not the nodes. This code works:


        if let touchLocation = touches.first?.location(in: self),
            let node = nodes(at: touchLocation).first {
            node.removeFromParent()
        }

Replies

Ah, figured it out.


First, hitTest is not the right method to determine a tap on a sprit. This is from the docs:


> this method searches for AR anchors and real-world objects detected by the AR session, not SpriteKit content displayed in the view. To search for SpriteKit objects, use the

nodes(at:)
method of the view's SpriteKit scene.


Second, in my original code, I was converting the touch to the view's coordinate system, not the nodes. This code works:


        if let touchLocation = touches.first?.location(in: self),
            let node = nodes(at: touchLocation).first {
            node.removeFromParent()
        }

Hey, I'm having a similar problem trying to tap on an SCNNode. Was wondering if you could help. I treid your solution but it's not working. If you could take a look at my question I would really appreciate it. Thanks!


https://stackoverflow.com/questions/48854151/detect-touch-on-scnnode-in-arkit