Lalit Prabhu
Lalit Prabhu

Reputation: 73

Touch on SKShapeNode not working

The code below is called in didBeginContact()

let retryButton = SKShapeNode(rectOfSize: CGSize(width: 100, height: 100))
retryButton.position = CGPoint(x: self.frame.width / 2 - 100, y: self.frame.height / 2 - 200)
retryButton.strokeColor = UIColor.redColor()
retryButton.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 100, height: 100))
retryButton.name == retryButtonName

worldNode.addChild(retryButton)

Here is how my touchesBegan() looks like

let touch = touches.first! as UITouch
let touchLocation = touch.locationInNode(worldNode)
let body: SKPhysicsBody? = self.physicsWorld.bodyAtPoint(touchLocation)

if body?.node?.name == retryButtonName {
    let scene = GameScene(size: self.size)
    self.view?.presentScene(scene)
    print("retry button is pressed")
}

Upvotes: 0

Views: 72

Answers (1)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71862

Assign name to your retryButton this way:

retryButton.name = retryButtonName

Upvotes: 1

Related Questions