Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touch through a node in SpriteKit

So I am working on making a tower defense game and have the tile board working and able to place towers... to a degree.

When a player clicks a button above the grid, it will toggle on the tower placement and allow the player to place towers on a cell within the grid. However, each tower is also getting a circle ShapeNode around it to handle detection of a creep moving into firing range. This shape node shows up just fine around the tower once I place them.

The logic I used to place the towers is that it finds the node you touch (named "cell") and replaces it with a tower node. But if the tower node has the circle node attached and covering a cell next to it, I am unable to select the cell node below the circle.

How would I go about touching "through" the circle, or setting its fill space to be nothing so that I can access the cells below it?

I'm still learning sprite-kit as I go, so if there may be a simpler approach feel free to point me in the right direction too.

like image 729
Klutch Avatar asked Mar 20 '23 21:03

Klutch


2 Answers

If you want find a node at a given point but not necessarily the deepest node you can use nodesAtPoint: available on SKNode including SKScene.

This allows you to find all child nodes of a scene, or node that intersect a given point. You could then filter this further to find the particular node you want, perhaps by filtering the array based on node class or node name.

A similar approach can be applied if looking for physics bodies at a particular point, using the method enumerateBodiesAtPoint:usingBlock: available in SKPhysicsWorld

It is worth noting that when using nodesAtPoint: the point is in the calling nodes coordinate system where as enumerateBodiesAtPoint:usingBlock: will always be in the scenes coordinate system.

like image 82
nacross Avatar answered Mar 22 '23 12:03

nacross


Without seeing any of your relevant code it's hard to say for certain but from your description, I think your issue is with the shape node. Instead of using a shape node to handle your contacts, I would suggest you use the tower's physics body instead. You can still have a circular physics body for your towers without resorting to adding a whole new node. Removing the shape node will also resolve your cross touch interference.

like image 23
sangony Avatar answered Mar 22 '23 11:03

sangony