我正在使用Swift和SpriteKit。
我遇到了以下的情况:
在这里,每个“三角形”都是一个SKShapenode。 我的问题是,我想检测当有人触摸屏幕时哪个三角形被触摸。 我假设所有这些三角形的碰撞框都是矩形,因此我的函数返回所有被触摸的碰撞框,而我只想知道哪一个实际上被触摸。
是否有任何方法可以使碰撞框完全匹配形状而不是矩形?
以下是我的当前代码:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
let touch = touches.first
let touchPosition = touch!.locationInNode(self)
let touchedNodes = self.nodesAtPoint(touchPosition)
print(touchedNodes) //this should return only one "triangle" named node
for touchedNode in touchedNodes
{
if let name = touchedNode.name
{
if name == "triangle"
{
let triangle = touchedNode as! SKShapeNode
// stuff here
}
}
}
}
enumerateChildNodesWithName
之后添加let shapenode = node as! SKShapeNode
,并删除if node is SKShapeNode
和if let p = (node as! SKShapeNode).path
,只保留if CGPathContainsPoint(shapenode.path, nil, touchPosition, false)
吗? - Drakalex