Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didBeginContact is being called multiple times for the same SKPhysicsBody

 func didBeginContact(contact: SKPhysicsContact) {
    if ( contact.bodyA.categoryBitMask & BodyType.shield.rawValue ) == BodyType.shield.rawValue  {
        contact.bodyB.node?.removeFromParent()
        counter++
        println(counter)


    } else if ( contact.bodyB.categoryBitMask & BodyType.shield.rawValue ) == BodyType.shield.rawValue {
        contact.bodyA.node?.removeFromParent()
        counter++
        println(counter)
    }
}

One physics body is from a texture shield.physicsBody = SKPhysicsBody(texture: shieldTexture, size: shieldTexture.size())

the other is from a circle sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.size.width/2)

When the tow objects contact each other sometimes sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.size.width/2) gets called multiple times. How do i get it to only get called once for each object even though i remove it from the parent as soon as it contacts.

like image 780
Bryan N Avatar asked Jan 15 '15 04:01

Bryan N


2 Answers

I have figured out how to get func didBeginContact(contact: SKPhysicsContact) to only be called once. This allows physics bodies with a texture SKPhysicsBody(texture: size:) to count collisions once even though in reality (because of the nature of the texture's physics body) this function will be called multiple times.

Step 1:

Create a name property for the SKSpriteNode (we will use ball for this example) and set it equal to a unique name. We can do this by creating a int

var number = 0 

ball.name = "ball \(number)"

This allows for a unique name evertime that object is created.

Step 2:

Create a array to hold these , append the ball to the array, increment the number

    var array: [String] = []
    var number = 0 

ball.name = "ball \(number)" 
array.append(ball.name!)
number ++

Step 3: Now in the func didBeginContact(contact: SKPhysicsContact) find out if the name is in the array. If it is increment the score, remove the node, and remove the name from the array. If the name is not in the array don't do anything.

Removing the name from the array allows us now to only count the function call once.

func didBeginContact(contact: SKPhysicsContact) {
    if ( contact.bodyA.categoryBitMask & BodyType.shield.rawValue ) == BodyType.shield.rawValue  {
        var name = contact.bodyB.node?.name!
        let index = find(array, name!)

        if contains(array, name!) {
            score++
            contact.bodyB.node?.removeFromParent()
            array.removeAtIndex(index!)
        }
    } 
}
like image 82
Bryan N Avatar answered Oct 20 '22 00:10

Bryan N


You can make it work without the array in this case. Instead of this:

contact.bodyA.node?.removeFromParent()
counter++

Use something like this:

if let node = contact.bodyA.node as? SKSpriteNode {
    if node.parent != nil {
        node.removeFromParent()
        counter++
    }
}

On the first contact you remove the node from the parent, on the subsequent calls the code in the if statement will be skipped.

like image 29
Konstantine Kalbazov Avatar answered Oct 20 '22 00:10

Konstantine Kalbazov