Jeremy Sh
Jeremy Sh

Reputation: 607

Trying to hide a node after contact was made

I have a SKSprite node that I want to make disappear after contact was made. I tried to set the .hidden = true but this did not work

var coin = SKSpriteNode()
//MARK: SKPhysicsContactDelegate methods

func didBeginContact(contact: SKPhysicsContact) {

    if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == objectCategory) {

        gameOver = 1
        movingObjects.speed = 0
        presentGameOverView()

    } else if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == coinCategory) {

        println("scoring")
        //code to hide node
        coin.hidden = true //not working

    }
}

}

Upvotes: 0

Views: 45

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

The SKPhysicsBody has a property called node. This can be accessed inside the didContactBegin function to hide the node.

func didBeginContact(contact: SKPhysicsContact) {

    if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == coinCategory) {

        println("scoring")
        //code to hide node
        contact.bodyB.node?.hidden = true // Changed

    }
}

Upvotes: 1

Related Questions