在触摸开始的地方添加节点

Add nodes where touches began

我正在用 Swift 制作游戏。 我需要在我按下屏幕的地方添加节点,我知道这不是来自其他星球,但我做不到。

高度简化的示例可能如下所示:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    let location = touch.location(in: self)
    let node = SKNode()
    node.position = location
    addChild(node)
}

您可以通过以下方式添加删除标签:

    class ViewController: UIViewController{
        private var labels = [UILabel]()
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            guard let touch = touches.first else {
                return
            }
            for label in labels {
                if label.frame.contains(touch.location(in: view)) {
                    label.removeFromSuperview()
                    return
                }
            }
            let label = UILabel()
            view.addSubview(label)
            labels.append(label)
            label.text = "touch"
            label.sizeToFit()
            label.center = touch.location(in: view)
        }
    }