GTG101
GTG101

Reputation: 149

How do i detect a touch location in SpriteKit?

I'm trying to create gaming and I want to be able to detect where a touch is onscreen. Ive seen several posts about this but all of them have outdated answers. Any help is appreciated.

Upvotes: 1

Views: 1585

Answers (1)

Whirlwind
Whirlwind

Reputation: 13675

When you create a new project and choose Game (SpriteKit game) if you look inside you will see that there is already shown how you handle touches:

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

As you can see, there is a variable location which represent the touch location in the scene's coordinate system.

Upvotes: 1

Related Questions