如何检测SKSpriteNode是否被触摸到

61

我想检测我的精灵节点是否被触摸,但我不知道从哪里开始。

let Pineapple = SKSpriteNode(imageNamed: "Pineappleimg")
Pineapple.userInteractionEnabled = true
Pineapple.position = CGPoint(x: CGRectGetMidX(self.frame) - 200, y: CGRectGetMidY(self.frame));
self.addChild(Pineapple)

这个问题可以作为一个例子:https://dev59.com/pHzaa4cB1Zd3GeqPSJtt - Emil
11个回答

0

这是我在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 nodoTouched = node as? YourNodeType {
            // touched!
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接