【问题标题】:How to detect precisely when a SKShapeNode is touched?如何准确检测何时触摸 SKShapeNode?
【发布时间】:2016-07-29 10:53:26
【问题描述】:

我正在使用 Swift 和 SpriteKit。

我有以下情况:

这里,每个“三角形”都是一个 SKShapenode。 我的问题是我想检测当有人触摸屏幕时触摸了哪个三角形。 我假设所有这些三角形的命中框都是矩形,所以我的函数返回所有触及的命中框,而我只想知道实际触及的是哪一个。

有没有什么方法可以让 hitbox 完全匹配形状而不是矩形?

这是我当前的代码:

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
            }
        }
    }
}

【问题讨论】:

    标签: ios swift sprite-kit shape skshapenode


    【解决方案1】:

    您可以尝试将CGPathContainsPointSKShapeNode 一起使用,而不是nodesAtPoint,这样更合适:

    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
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 这正是我想要的!并且所有名为“triangle”的节点都是SKShapeNodes,我们可以在enumerateChildNodesWithNamelet shapenode = node as! SKShapeNode之后添加,并删除if node is SKShapeNodeif let p = (node as! SKShapeNode).path只是为了有` if CGPathContainsPoint(shapenode.path, nil, touchPosition, false)`?
    • 不安全, enumerateChildNodesWithName 与通用 SKNode 一起使用 ;) ,看看苹果指南:developer.apple.com/reference/spritekit/sknode/…
    【解决方案2】:

    这是最简单的方法。

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        for touch in touches {
            let location = touch.locationInNode(self)
            if theSpriteNode.containsPoint(location) {
                 //Do Whatever    
            }
        }
    }
    

    【讨论】:

    • 这是如何通过三角形找到被触摸的?
    【解决方案3】:

    我使用 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 //...
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      相关资源
      最近更新 更多