8

在过去的两天里,我一直在努力让两个 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********************")
}
4

4 回答 4

10

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

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

我有一个来自 swift 2 的代码,'didBeginContact' 坐在那里,但没有被调用。在相当白之后,我发现功能已更改。所以,我认为我的回答可以帮助别人。

于 2017-01-11T13:44:26.030 回答
3

如果你想在 car 和 slowCar 之间建立联系,你必须初始化两个physicsBodies 的 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)
于 2014-10-18T18:37:50.350 回答
2

It turned out to be a simple problem. In my original code I was setting parameters for the SKPhysicsBody detection frame like so:

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

Similarly I was doing the same for the second node that I was testing physics collisions for.

Simply removing the 'centre:' parameters like so:

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

for the two sprite nodes solved the problem and the nodes now crash into each other and push themselves aside as expected.

于 2014-10-19T09:06:48.087 回答
1

请注意,不会检测到两个静态物体之间的接触

(node.physicsBody?.isDynamic = false)
于 2016-12-05T13:27:51.940 回答