2 Replies
      Latest reply on Feb 18, 2018 9:42 AM by alanr917
      gmcerveny Level 1 Level 1 (0 points)

        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()
                }
        
        • Re: Detect tap on ARKit SKNode
          gmcerveny Level 1 Level 1 (0 points)

          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()
                  }