Touching moving Sprites in SpriteKit

Viewed 99

I'm creating a SpriteKit game that involves touching falling targets. Currently, the targets are too difficult to catch on first touch (touchesBegan:), and only seem to be touchable by positioning your finger ahead of time (touchesMoved:). Is there a technique for dampening touches or widening the touch location to make the first touch more effective? My code looks something like this right now:

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    let positionInScene = touch.location(in: self)
    print(positionInScene)
    guard let touchedNode = self.nodes(at: positionInScene).first as? SKSpriteNode else { return }
    if let dot = touchedNode.name {
        if dot == "dot" {
            removeTarget(touchedNode)
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    let positionInScene = touch.location(in: self)
    print(positionInScene)
    guard let touchedNode = self.nodes(at: positionInScene).first as? SKSpriteNode else { return }
    if let dot = touchedNode.name {
        if dot == "dot" {
            removeTarget(touchedNode)
        }
    }
}
1 Answers

Are your entities too small? With the given info, I recreated a small scene in a playground, and all is working as expected. If I have a presented child node, defined as

let circle = SKShapeNode(circleOfRadius: 20)

And I have defined both the physicsWorld of the SKScene, and physicsBody of the SKNode, with the given touchesBegan, there is no problem in detecting a collision.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let location = touches.first?.location(in: self) 
       if self.atPoint(location) === circle {
          print("TOUCH")
       }
    }
}
Related