Reputation: 171
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.
Upvotes: 4
Views: 2316
Reputation: 93
TouchesBegan automatically detects all nodes at that position, you just have to loop through them.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
CGPoint touchPoint = [touch locationInNode:self];
NSArray *nodes = [self nodesAtPoint:touchPoint];
if ([nodes count])
{
for (SKNode *node in nodes)
{
if ([node.name isEqualToString:@"cell"])
{
}
else
{
}
}
}
}
}
"self" being the node that touches began is called in (most likely your scene). You can also filter it by parent objects, for example; if you add all of your nodes to a parent node named "background", you can filter to only the children of the background node by replacing "self" with your background node (though this will result in it using the background nodes coordinates).
Upvotes: 0
Reputation: 2043
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.
Upvotes: 5
Reputation: 11696
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.
Upvotes: 3