Search code examples
swiftxcodenodestouchesbegan

Add nodes where touches began


I´m making a game with Swift. I need to add nodes where I press the screen, I know this is not from other planet but I can't do it.


Solution

  • Highly simplified example could look like this:

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