如何检测是否已触摸 SKSpriteNode

How do I detect if an SKSpriteNode has been touched

我正在尝试检测我的 sprite 节点是否已被触摸,但我不知道从哪里开始。

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

首先将SKSpriteNodename属性设置为字符串

pineapple.name = "pineapple"
pineapple.userInteractionEnabled = false

然后在touchesBegan函数中Scene

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch:UITouch = touches.anyObject()! as UITouch
    let positionInScene = touch.locationInNode(self)
    let touchedNode = self.nodeAtPoint(positionInScene)

    if let name = touchedNode.name
    {
        if name == "pineapple"
        {
            print("Touched")
        }
    }

}

这是一种方法。
您还可以子类化 SKSpriteNode 并覆盖其中的 touchesBegan

class TouchableSpriteNode : SKSpriteNode
{
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        print("touched")
    }
}

然后

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

实现触摸开始时调用的 touchesBegan 方法。或者,您也可以在 touchesEnded 中执行此操作。

override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
    let touch = touches.anyObject() as UITouch
    let location = touch.locationInNode(self)
    let nodes = self.nodesAtPoint(location)

    for node in nodes
    {
        if node.name == "youNodeName"
        {
            // Node tapped
            // Do something

            break
        }
    }
}

使用这段代码来检测 SKSpriteNode 上的触摸

if(nodeAtPoint(location) == node){



}

如果您只想寻找几个可以触摸的节点(例如,游戏中的 "Continue" 或 "Exit" 标签 UI),这可能是一个替代方案,但是非常简单的解决方案:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first!
    if myNode.containsPoint(touch.locationInNode(self)) {
        print("touched")
    }
}

更新 Swift 3.0 和 XCode 7.3.1。我有一个 SKShapeNode 派生到一个新的 class 并将其插入到场景中。当我想检测这个对象时,我检查如下:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch in touches {

        let location = touch.locationInNode(self)
        let nodes = self.nodesAtPoint(location)

        for node in nodes
        {
            if node is SKNodeDerivedNode
            {
                NSLog("Touch a SKNodeDerivedNode")
                break
            }
        }
    }
}

Swift 3SKSpriteNode 的子类中嵌入触摸功能的答案:

class SpriteSub: SKSpriteNode {

    init() {
        super.init(texture: nil, color: UIColor.red, size: CGSize(width: 50, height: 50))
        isUserInteractionEnabled = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    ...

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touch!")
    }

}

更新 Swift Swift 版本 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1) 和 XCode 版本8.2.1 (8C1002):

Value of type 'Set' has no member 'anyObject'

'locationInNode' has been renamed to 'location(in:)'

'nodeAtPoint' has been renamed to 'atPoint(_:)'

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let node : SKNode = self.atPoint(location)
        if node.name == "myNodeName" {
            print("Hello")
        }
    }
}

这将在 Xcode 9.2 Swift 4.0

中检测触摸
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
    let touch:UITouch = touches.first!
    let positionInScene = touch.location(in: self)
    let touchedNode = self.atPoint(positionInScene)

    if let name = touchedNode.name
    {
        if name == "playLbl"
        {
            print("playLbl Touched")
        }
    }

}

这是我在Swift4中使用的方法来查找特定类型的节点是否有触摸:

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

如果子类化 SKSpriteNode 后您的 sprite 仍然没有工作,您很可能在初始化时忘记添加 node.isUserInteractionEnabled = true

这允许调用 touchesBegan(_:with:),因为您现在可以与节点交互。


示例:

node = MySprite(texture: texture, size: size)
node.isUserInteractionEnabled = true

swift 5 更新

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        for touch in touches {
            let location = touch.location(in: self)
            let touchedNode = self.nodes(at: location)
            for node in touchedNode {
                if node.name == "play_button" {
                    startGame()
                }
            }
        }
    }