我想搜索圆内的节点-该圆是SKShapeNode。返回节点(SKSpriteNode)应该是在圆内找到的具有最低y值的节点。
对于感兴趣的人来说,这是我用于搜索的代码:
SKSpriteNode *currentNode = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(0, 0)];
[currentNode setPosition:CGPointMake(0, 99999)];
SKShapeNode *circle = [SKShapeNode shapeNodeWithCircleOfRadius:100];
// adding circle and other nodes to scene
[self enumerateChildNodesWithName://* usingBlock:^(SKSpriteNode *foundNode, BOOL * _Nonnull stop) {
if ([circle containsPoint:currentNode.position]) {
if (currentNode.position.y > foundNode.position.y) {
currentNode = foundNode;
}
}];
// currentNode = node inside circle with lowest y
我不喜欢这个解决方案,看起来只找到一个节点需要花费太多的精力。我还尝试使用nodesAtPoint/nodeAtPoint,但这在我的项目中不起作用——"大"子节点。
我很好奇:有没有一种更容易的方法来搜索具有这样细节的节点?
检查Y坐标将比[circle containsPoint]快得多,因此您可能需要更改条件的顺序,以便首先比较候选节点的Y坐标,然后只检查圆包含点。对于在圆的最低点(-100)和当前节点的Y坐标之间有Y坐标的节点。
类似于:
if ( foundNode.position.y > -100
&& foundNode.position.y < currentNode.position.y )
{
if ([circle containsPoint:foundNode.position])
{ currentNode = foundNode }
}
注意:我假设enemy.position实际上是foundNode.position。