7

我一直在努力爭取在過去兩天得到兩個SKSpriteNodes來註冊衝突並喚起didBegin#contactiOS Swift didBeginContact未被調用

我已經爲這兩個對象設置了位掩碼'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********************") 
} 
+1

代碼轉儲不在此平臺上讚賞。 – Mundi 2014-10-18 10:37:50

+0

注意**以及collisionBitMask,你必須設置contactTestBitMask **。這是通常的gotchya。請注意,在故事板編輯器中,它將contactTestBitMask保留爲默認值0--這是通常的問題。只需將其設置爲「1」 – Fattie 2017-10-23 21:35:28

回答

2

原來是一個簡單的問題。在我的原代碼我是爲SKPhysicsBody檢測幀像這樣設置參數:

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

同樣地,我在做同樣對於我測試物理碰撞對第二個節點。

只要去掉「中心:」參數,像這樣:

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

兩個精靈節點解決了這個問題,並在節點現在撞上對方和自己推一旁預期。

3

如果你想使汽車和slowCar之間的接觸,你必須初始化兩個physicsBodies的categoryBitMask(我認爲你做)。請參閱下面的代碼以獲得兩個physicsBody之間的聯繫。當有聯繫時,它返回你的顯示功能:

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

是否適合您? – Haox 2014-10-19 08:17:33

+0

謝謝,不幸didBeginContact()並沒有被首先引起。我剛剛剝離了整個代碼,以便它將精靈放在屏幕上,設置物理引擎,然後運行一個動作讓其中一個碰到另一個,但不幸的是它仍然無法工作。 – Headstock67 2014-10-19 08:31:03

+0

您是否設置了'car'和'slowCar'的categoryBitMask?在你的代碼中,你設置了「carBody」和「slowCarBody」。 – Haox 2014-10-19 08:58:08

0

請注意,聯繫人將不是兩個靜態物體

(node.physicsBody?.isDynamic = false) 
8

「didBeginContact」已經在迅速3

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

被改爲「didBegin」之間可以檢測我有從代碼swift 2和'didBeginContact'坐在那裏,但沒有被調用。經過相當一段白色,我發現功能已經改變。所以,我認爲我的回答可以幫助某人。

相關問題