I'm starting to learn Swift2 for iOS development using SpriteKit, and I cannot find that I have touched the visible part of my sprite.
I can find which node was affected as follows:
let touchedNode = self.nodeAtPoint(location)
But it detects a touch of sprite. I would like to find a touch on the "opaque" part of the sprite.
I tried creating a PhysicalBody with an alpha mask bounding box and checking if the selected frame of the selected node area contains the location of the touch as follows:
sprite.physicsBody = SKPhysicsBody.init(texture: sprite.texture!, size: sprite.size) sprite.physicsBody?.affectedByGravity = false if (touchedNode.containsPoint(location)){
But it did not help. If I click the transparent part of my sprite, the event is still triggered.
The documentation reads: โA new physical object has been created that includes all the texels in the texture that have non-zero alpha values,โ so should this not work?
Thank you for your time.
PS: I also tried to be more generous on my alpha threshold, but it also did not work (in case my transparency was not perfect).
UPDATE:
To add a little more detail, I create a level editor. This means that I will create many nodes, depending on what the user selects, and I need to be able to select / move / rotate / etc. These nodes. I actually use SKSpriteNodes, since my PNG images are automatically added to xcassette in this way. I decided to use the PhysicalBody Alpha Mask bounding box, as this value is shown in the scene editor (Xcode) when selecting node, and when selected, the Alpha Mask selects exactly the part of my sprite that I want to be able to detect touch inside.
If I use the wrong ideas / methods, please tell me. This is possible as I am just starting to use Swift and SpriteKit.
UPDATE 2
I requested physics (as recommended) to get the correct physical device and got the name of the attached node as follows:
let body = self.physicsWorld.bodyAtPoint(location) print(body?.node?.name)
But it still prints the name node, even if I touch the outer frame, which makes no sense to me.
Thank you for your help.