如何检测spritekit中的触摸?

Asp*_*hys 2 xcode touch sprite-kit swift

我有一个精灵,如果按下精灵,我想打印一些文字.我在其上找到的每个教程似乎都已过时.这怎么可能?

它应该是这样的:

if Sprite is touched {
    print("Some Text")
}
Run Code Online (Sandbox Code Playgroud)

Luc*_*tti 5

如果您的Sprite有自定义类,则只需覆盖该touchesBegan方法

class Player: SKSpriteNode {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        userInteractionEnabled = true
    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("Did touch Player sprite")
    }
}
Run Code Online (Sandbox Code Playgroud)

别忘了设置 userInteractionEnabled = true