ItsMeAgain
ItsMeAgain

Reputation: 595

How can I increase the area of a sprite only for touch recognition?

My sprites are quite small so occasionally taps are not very accurate. I would like to know how I can increase the area around the sprite for touch recognition only so that other stuff like physics are not affected?

For example, say my sprite sizes are 40x40 and I want the touch to be recognized by a larger area of 60x60 for a given sprite.

Please see code below:

func singleTapHandler(recognizer:UITapGestureRecognizer){

    let locationInView = recognizer.location(in: self.view)

    let locationInScene = self.convertPoint(fromView: locationInView)

    print(locationInScene)

   let node = atPoint(locationInScene)

    if let nodeName = node.name {
        if nodeName = "group1"  {
          fliesGroup1.changePosition()
        }
    }

}

UPDATE Based on Hola's answer I did the following to create my empty SKNode but it isn't recognized when I use the flyTouchName to check for location in scene with the code above.

var touchNode = SKNode()
touchNode.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 60, height: 60))
touchNode.physicsBody?.collisionBitMask = 0
touchNode.isUserInteractionEnabled = true
touchNode.physicsBody?.contactTestBitMask = 0
touchNode.name = flyTouchName
fly.addChild(touchNode)

Upvotes: 0

Views: 75

Answers (1)

hola
hola

Reputation: 3500

You could create an empty SKNode, set it's dimensions, enable user interaction, and add it as a child of your sprite.

Then use that node as your input handler.

Upvotes: 2

Related Questions