Matthew Jacobsen
Matthew Jacobsen

Reputation: 43

How to detect if I tapped a SKShapeNode

override func didMove(to view: SKView) {

    view.scene?.anchorPoint = CGPoint(x: 0,y : 0)

    castle = SKShapeNode(circleOfRadius: ballRadius1)
    castle.physicsBody = SKPhysicsBody(circleOfRadius:ballRadius1)
    castle.fillColor = .white
    castle.name = "castle"
    castle.position = CGPoint(x: 0, y: 0)
    castle.physicsBody?.isDynamic = false
    castle.physicsBody?.affectedByGravity = false;
    castle.isUserInteractionEnabled = true
    self.addChild(castle)

I have a circle in the middle of the screen, I want to make it disappear when I tap it. Can you please help me? It is an SKShapeNode and has a PhysicsBody with the same radius

Upvotes: 1

Views: 345

Answers (1)

sicvayne
sicvayne

Reputation: 620

You could use touchesBegan and make it look like so

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


    for touch: AnyObject in touches{

        let pointOfTouch = touch.location(in: self)
        let nodeITapped = atPoint(pointOfTouch)
        let nameOfTappedNode = nodeITapped.name

        if nameOfTappedNode == "castle"{
            //make it do whatever you want
            castle.removeFromParent()

        }

    }


}

you could also implement it in touchesEnded instead if you want the node to disappear as soon as the user releases the touch.

Upvotes: 2

Related Questions