w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
iOS Swift didBeginContact not being called

If you want make a contact between car and slowCar you have to init the categoryBitMask of both physicsBodies (I think you did). See the code below to get contact between two physicsBodies. When there is a contact it returns your display function :

//init your categoryBitMask : 
let carCategory:UInt32 = 0x1 << 0
let SlowCarCategory:UInt32 = 0x1 << 1


//init car
car.physicsBody?.categoryBitMask = carCategory
car.physicsBody?.contactTestBitMask = slowCarCategory

//init slowCar 
slowCar.physicsBody?.categoryBitMask = slowCarCategory
slowCar.physicsBody?.contactTestBitMask = CarCategory

// set your contact function
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 & carCategory) != 0 &&
(secondBody.categoryBitMask & slowCarCategory) != 0)
    {
        displayfunction(firstBody.node as SKSpriteNode, car:
secondBody.node as SKSpriteNode)
    }
}

func displayFunction (slowCar : SKSpriteNode, car : SKSpriteNode)




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.