Search code examples
swiftcocos2d-iphonecollisiondetectionspritebuilder

How do I, in swift, use ccPhysicsCollisionBegin to find the collision between a specific type of object and a singular instance of another object?


For example, let's say I have a block being hit with several balls. How would I make it so the function gets run when any ball hits the block? (as opposed to a singular ball hitting the block)

Essentially, I want this:

func ccPhysicsCollisionBegin(pair: CCPhysicsCollisionPair!, AllBalls[] nodeA: CCNode!, block nodeB: CCNode!) -> Bool {

    return true
}

Solution

  • If all your balls are objects of a Ball class, you can set the collisionType property of the ball's physics body to ball.

    For example:

    func didLoadFromCCB() {
       physicsBody.collisionType = "ball"
    }
    

    Then, each ball object that you create will have that same collision type and the following function will be called whenever any of the balls collide with the block:

    func ccPhysicsCollisionBegin(pair: CCPhysicsCollisionPair!, 
                                 ball: CCNode!, block: CCNode!) -> ObjCBool {
            return true
        }