Swift 3 测试SKNode的SKSpriteNode子类是否包含触摸

Swift 3 test if SKSpriteNode subclass of SKNode contains touches

class MainNode:SKNode {
    class Box: SKSpriteNode {
        override init(texture: SKTexture?, color: UIColor, size: CGSize) {
            super.init(texture: SKTexture(imageNamed: "testIMG1"), color: UIColor.clear, size: CGSize(width: 50, height: 50))
        }
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    var subclassNode: SKSpriteNode!
    var box = Box()

    convenience init(size: CGSize) {
        self.init()

        box = Box()
        box.position.x = 0

        subclassNode = SKSpriteNode(color: UIColor.red, size: CGSize(width: 40, height: 40))
        subclassNode.position.x =  0
        subclassNode.zPosition = 6

        self.addChild(subclassNode)
    }
}

在我的 class MainNode 中,我正在尝试测试 SKSpriteNode subclass Box 是否包含我的 GameScene

中的触摸

添加

for touch in touches {
    let location = touch.location(in: self)
    if MainNode.SubclassNode.contains(location) {
        print("Subclass contains touch")
    }
}

我的 GameScene class 或我的 MainNode class 似乎根本不起作用,即使 SubclassNode.isUserInteractionEnabled = true

如何确定触摸是否在我的子class中?

我在 KnightOfDragon

的更多研究和帮助下弄明白了

我设置了self.isUserInteractionEnabled = true,然后给我的子class节点取了一个名字subclassNode.name = "SubclassNode",然后我将这段代码添加到我的MainNode class

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let touchedNode = self.atPoint(location)
        if let name = touchedNode.name
        {
            if name == "SubclassNode" {
                print("touched Subclass Node")
            }
        }
    }
}