Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit - Ignore transparency when selecting node

Tags:

sprite-kit

I have an SKSpriteNode that has a texture assigned like this:

node.texture = SKTexture(imageNamed: "Oval")

Users can select this object and drag it around. I use the following to identify when it is being selected in the touchesMoved function.

var touchedNode = allObjects.nodeAtPoint(location)

The problem is that almost half the surface area of this image file is transparent. However, nodeAtPoint responds to touches on the transparency.

Does anyone know of a way to ignore the transparency?

like image 335
Sparked Avatar asked Sep 11 '15 12:09

Sparked


1 Answers

In your touches Moved you can get the color of the pixel of the node you touched and then using an if statement you can ignore the node if the color is transparent (has an alpha of 0). Check here to see how to get the color

SpriteKit: How can I get the pixel color from a point in SKSpriteNode?

like image 190
AwesomeTN Avatar answered Sep 20 '22 18:09

AwesomeTN