0
votes

I'm doing a shooting game the problem i have so far is that when the bullet collides with the Enemy and call the function update score, they call the function around 10 times, heres my didBeginContact:

func didBeginContact(contact: SKPhysicsContact) {
    var firstBody :SKPhysicsBody
    var secondBody :SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
    {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    }
    else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }


    if (firstBody.categoryBitMask == CollisionCategories.Bullet) &&
        (secondBody.categoryBitMask == CollisionCategories.Enemy) {

            println("Hit")
            firstBody.node!.removeFromParent()
            secondBody.node!.removeFromParent()
            //change score
            updateScore(1)

    }
}
1

1 Answers

0
votes

The problem i had is that the Enemy had its physicsBody configured as a texture of size, by changing it from texture to circleOfRadius, worked fine.

From:

Enemy.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())

To:

Enemy.physicsBody = SKPhysicsBody(circleOfRadius: (Enemy.size.width/2))