iOS Swift didBeginContact未被调用

时间:2014-10-18 09:16:58

标签: swift sprite-kit collision-detection skphysicsbody skphysicsworld

过去两天我一直在努力让两个SKSpriteNodes注册一个碰撞并唤起didBegin#contact

我为两个对象设置了位掩码“categoryBitMask”,“contactTestBitMask”和“collisionTestBitMask”。

我还将“dynamic”属性设置为“true

initPhysics()似乎设置了physicsWorld。

我所期待的是didBegin#Contact被调用,但它不是

//Set up Physicsbody bit masks
let playerCarBitMask: UInt32 = 0x1 << 1
let slowCarBitMask: UInt32 = 0x1 << 2



//initPhysics
func initPhysics() {
    println("(((((((((((((( Initiating Physicsbody ))))))))))))))")
    self.physicsWorld.contactDelegate = self
    self.physicsWorld.gravity = CGVector.zeroVector

    println("self.physicsWorld.contactDelegate = \(self.physicsWorld.contactDelegate)")

}

//setupPlayer
func setupPlayer() {

    car = SKSpriteNode(imageNamed: "redCarUp")
    car.setScale(2.0)
    car.position = CGPoint(x: 800, y: 400)
    car.zPosition = 100
    car.name = "car"

    gameNode.addChild(car)

    let carBody = SKPhysicsBody(
        rectangleOfSize: car.frame.size, center: car.position)

    carBody.dynamic = true
    carBody.categoryBitMask = playerCarBitMask
    carBody.contactTestBitMask = slowCarBitMask
    carBody.mass = 5
    carBody.collisionBitMask = slowCarBitMask
    car.physicsBody = carBody

    println("carBody = \(carBody)")
    println("carBody.dynamic = \(carBody.dynamic)")
    println("carBody.mass = \(carBody.mass)")
    println("carBody.categoryBitMask = \(carBody.categoryBitMask)")
    println("carBody.contactTestBitMask = \(carBody.contactTestBitMask)")
    println("carBody.collisionBitMask = \(carBody.contactTestBitMask)")



    slowCar = SKSpriteNode(imageNamed: "blueCarUp")
    slowCar.setScale(2.0)
    let slowCarScenePos = CGPoint(
        x: 680,
        y: 2048)


    slowCar.position = gameNode.convertPoint(slowCarScenePos, fromNode: self)
    println("slowCar.position = \(slowCar.position) ****")
    slowCar.zPosition = 80
    slowCar.name = "slowCar"



    let slowCarBody = SKPhysicsBody(
        rectangleOfSize: slowCar.frame.size, center: slowCar.position)


    println("slowCar = \(slowCar) ****")

    slowCarBody.dynamic = true
    slowCarBody.categoryBitMask = slowCarBitMask
    slowCarBody.contactTestBitMask = playerCarBitMask
    slowCarBody.mass = 5
    slowCarBody.collisionBitMask = playerCarBitMask
    slowCar.physicsBody = slowCarBody
    gameNode.addChild(slowCar)



}

func didBeginContact(contact: SKPhysicsContact!) {
    println("*******************PhysicsContact********************")
}

3 个答案:

答案 0 :(得分:8)

'didBeginContact'已在swift 3中更改为'didBegin'

func didBegin(_ contact: SKPhysicsContact) { //stuff }

我有来自swift 2的代码,'didBeginContact'坐在那里,但没有被调用。经过一番白色,我发现功能已经改变了。所以,我认为我的回答可以帮助别人。

答案 1 :(得分:3)

如果你想在car和slowCar之间建立联系,你必须初始化这两个物理实体的categoryBitMask(我想你做过)。请参阅以下代码以获取两个物理实体之间的联系。当有联系人时,它会返回您的显示功能:

//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)

答案 2 :(得分:2)

原来这是一个简单的问题。在我的原始代码中,我正在设置SKPhysicsBody检测框架的参数,如下所示:

let carBody = SKPhysicsBody( rectangleOfSize: car.frame.size, center: car.position)

同样地,我正在为我测试物理碰撞的第二个节点做同样的事情。

只需删除'center:'参数,如下所示:

let carBody = SKPhysicsBody(rectangleOfSize: car.frame.size)

两个精灵节点解决了这个问题,节点现在互相撞击,并按预期将自己推到一边。