Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何精确地检测何时触摸到SKShapeNode?_Ios_Swift_Sprite Kit_Shape_Skshapenode - Fatal编程技术网

Ios 如何精确地检测何时触摸到SKShapeNode?

Ios 如何精确地检测何时触摸到SKShapeNode?,ios,swift,sprite-kit,shape,skshapenode,Ios,Swift,Sprite Kit,Shape,Skshapenode,我在和斯威夫特和斯皮特基特合作 我有以下情况: 这里,每个“三角形”都是一个SKShapenode。 我的问题是,我想检测当有人触摸屏幕时,哪个三角形正在被触摸。 我假设所有这些三角形的点击框都是矩形,所以我的函数会返回所有被点击的点击框,而我只想知道哪一个被点击 有没有办法让一个点击框完全匹配形状而不是矩形 以下是我当前的代码: override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)

我在和斯威夫特和斯皮特基特合作

我有以下情况:

这里,每个“三角形”都是一个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
            }
        }
    }
}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?)
{
让我们先接触
让touchPosition=touch!.locationInNode(自)
让touchedNodes=self.nodesatapoint(touchPosition)
print(touchedNodes)//这应该只返回一个名为node的“三角形”
对于touchedNodes中的touchedNode
{
如果let name=touchedNode.name
{
如果名称==“三角形”
{
设三角形=接触节点为!SKShapeNode
//这里的东西
}
}
}
}

您可以尝试将
CGPathContainsPoint
SKShapeNode
一起使用,而不是
nodesattpoint
,这更合适:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
    let touch = touches.first
    let touchPosition = touch!.locationInNode(self)
    self.enumerateChildNodesWithName("triangle") { node, _ in
        // do something with node
        if node is SKShapeNode {
            if let p = (node as! SKShapeNode).path {
                if CGPathContainsPoint(p, nil, touchPosition, false) {
                    print("you have touched triangle: \(node.name)")
                    let triangle = node as! SKShapeNode
                    // stuff here
                }
            }
        }
    }
}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?)
{
让我们先接触
让touchPosition=touch!.locationInNode(自)
self.enumerateChildNodesWithName(“triangle”){node,uIn
//用node做点什么
如果节点是SKShapeNode{
如果让p=(节点为!SKShapeNode).path{
如果CGPathContainsPoint(p,nil,touchPosition,false){
打印(“您已接触三角形:\(node.name)”)
设三角形=节点为!SKShapeNode
//这里的东西
}
}
}
}
}

这是最简单的方法

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
    for touch in touches {
        let location = touch.locationInNode(self)
        if theSpriteNode.containsPoint(location) {
             //Do Whatever    
        }
    }
}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?)
{
接触{
let location=touch.locationInNode(自)
如果主节点包含点(位置){
//做任何事
}
}
}

我使用Swift 4的方式:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { 
     return 
    }
    let touchPosition = touch.location(in: self)
    let touchedNodes = nodes(at: touchPosition)
    for node in touchedNodes {
        if let mynode = node as? SKShapeNode, node.name == "triangle" {
            //stuff here
            mynode.fillColor = .orange //...
        }
    }

}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
guard let touch=touch.first else{
返回
}
让touchPosition=触摸位置(in:self)
让touchedNodes=节点(位于:touchPosition)
对于touchedNodes中的节点{
如果让mynode=node为?SKShapeNode,node.name==“三角形”{
//这里的东西
mynode.fillColor=.orange/。。。
}
}
}

这正是我想要的!所有名为“triangle”的节点都是skshapenode,我们可以在
enumerateChildNodesWithName
之后添加吗!SKShapeNode,如果节点是SKShapeNode,则删除
,如果让p=(节点为!SKShapeNode)。path
只是为了“如果CGPathContainsPoint(shapenode.path,nil,touchPosition,false)”?这不安全,EnumerateChildNodeWithName与通用SKNode一起工作;),看看苹果指南:这是如何通过三角形来找到哪个被触摸的?