在指定区域中查找SKNodes

时间:2018-03-20 18:22:31

标签: ios swift sprite-kit

我希望能够在SpriteKit中执行与nodes(at:)函数类似的操作。但是,对于我的用例,检查一个特定CGPoint上是否存在SKNode是太具体了。

我想要的是一个区域(我可以指定),类似于nodes(at:)函数,在那里我找回与特定区域相交的节点数组

有些功能可以帮我吗?另外,我希望将任何SpriteKit Physics带入其中。

1 个答案:

答案 0 :(得分:1)

您正在寻找的是

func intersects(_ node: SKNode) -> Bool
  

返回一个布尔值,指示此节点是否与指定节点相交。如果两个节点的帧相交,则认为这两个节点相交。在此测试中将忽略两个节点的子节点。

...示例

//sprite created in Scene editor
if let locationBox = self.childNode(withName: "locationBox") as? SKSpriteNode {
    self.locationBox = locationBox
}

//sprite created programmatically 
let locationBox = SKSpriteNode(color: .red, size: CGSize(width: 300, height: 300))
locationBox.zPosition = 1
locationBox.position = CGPoint(x: 100, y: 100)
addChild(locationBox)

检查交叉点

for child in children { 
    if locationBox.intersects(child) {
        print("child.name \(child.name)")
    }
}