我如何检测 SpriteKit 中的触摸位置?

How do i detect a touch location in SpriteKit?

我正在尝试创建游戏,我希望能够检测屏幕上的触摸位置。我看过几篇关于此的帖子,但所有帖子都有过时的答案。 感谢您的帮助。

当您创建一个新项目并选择游戏(SpriteKit 游戏)时,如果您查看内部,您会看到已经显示了您如何处理触摸:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
       /* Called when a touch begins */

        for touch in touches {
            let location = touch.locationInNode(self)

            let sprite = SKSpriteNode(imageNamed:"Spaceship")

            sprite.xScale = 0.5
            sprite.yScale = 0.5
            sprite.position = location

            let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)

            sprite.runAction(SKAction.repeatActionForever(action))

            self.addChild(sprite)
        }
    }

如您所见,有一个变量location表示场景坐标系中的触摸位置。