How to refine the shape of an SKSpriteNode for tap gestures?

In my app I create mutiple SKSpriteNodes, gems. Code snippet 1

When I loop through nodes from the main scene in a tap gesture, Code snippet 2, the gems register in a perfect square shape even though they are not. I have highlighted all areas that the orange gem registers in a tap as white. Here is the screen shot http://98.7.37.117/gem.png

Since the gem is itself not a square, I'd like to know if there is a way refine its shape so it would only show up in the list of nodes in UITapGestureRecognizer if the orange part is tapped. I have even tried by assigning it a physicsBody. But that made no difference.

Code Snippet 1

class MyGem : SKSpriteNode{

    init(iColor : Gems) {
        fileName = "\(iColor)_gem"
        let skTexture = SKTexture(imageNamed: fileName)
        super.init(texture: skTexture, color: .clear, size: .zero)
        self.isHidden = true
        self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        self.size = CGSize(width: myGV.gemSize.width, height: myGV.gemSize.height)
        self.zPosition = theZ.gem
        self.position = CGPoint(x: 0, y: 0 )
        self.isUserInteractionEnabled = false
        self.name = "gem"

    }

    required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
    }
}

Code Snippet 2

    @objc func tappedView(_ sender:UITapGestureRecognizer) {

        if sender.state == .ended{
            var post = sender.location(in: sender.view)
            post = self.convertPoint(fromView: post)
            for node in self.nodes(at: post){
                if let touchNode = node as? MyGem 
                    print("touched gem")

                    highliteGem(theGem: touchNode, clearAll: false)
                    return
                }

ignore the self.isHidden line. I set that to true false later on.

We can now include images, it is easier to have them in the post directly.

I would try the following in tappedView: test where exactly the tap was and compare to the orange shape. If d is the semi width (and height) of the full square (position of orange top), the parts to exclude are: top left triangle : cond1 = x + y < d top right triangle : cond2 = x - y > d bottom right triangle : cone3 = x + y > 3 d bottom left triangle cond4 = y - x > d

                if let touchNode = node as? MyGem 
                    print("touched gem")
                    if cond1 || cond2 || cond3 || cond4 {
                           return   // without doing anything
                    }
                    // else, OK
                    highliteGem(theGem: touchNode, clearAll: false)
                    return
                }

Lots of calls ?

Don't understand your point. Just 4 values to compute and evaluate.

Is it part of SpriteKit ?

No that's additional code to test where you hit.

Surely it can't view every Node as a rectangle or square?

What do you mean ?

Surely it can't view every Node as a rectangle or square?

What do you mean ?

I mean when you create a spritenode from an image that the node’s area becomes a uniform area that goes beyond the image.

ie, look at the orange diamond. Why does the white area (which I drew in for this post) become part of the area the spritenode is part of?

AFAIK, the frame is a rectangle.

As you hinted previously, you can shape it using alpha channel:

https://developer.apple.com/documentation/spritekit/skphysicsbody/shaping_a_physics_body_to_match_a_node_s_graphics

Hence, it will not react to tap outside the shape.

Nevertheless, I find that for simple shape as diamond, the test I proposed is really simple.

Hence, it will not react to tap outside the shape.

While SKPhysicsBody(texture: sprite.texture!, CGSize) does create the physics shape, UITapGestureRecognizer is not affected by it. It sees the tap in the rectangle/square area.

Nevertheless, I find that for simple shape as diamond, the test I proposed is really simple.

But what happens when I am doing a more complex SKSpriteNode? Is this the way Swift handles this?

How to refine the shape of an SKSpriteNode for tap gestures?
 
 
Q