iOS Swift didBeginContact未被调用

前端之家收集整理的这篇文章主要介绍了iOS Swift didBeginContact未被调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在过去的两天里,我一直在努力让两个SKSpriteNodes注册一个碰撞并唤起didBegin #contact.

我为两个对象设置了它们的位掩码’categoryBitMask’,’contactTestBitMask’和’collisionTestBitMask’.

我还将’动态’属性设置为’true’

initPhysics()似乎设置了physicsWorld.

我所期待的就是调用了#Begin #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********************")
}

解决方法

‘didBeginContact’已在swift 3中更改为’didBegin’

func didBegin(_ contact:SKPhysicsContact){
//东西
}

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

猜你在找的iOS相关文章