didBeginContact not called



我正在制作一个游戏,我需要我的spriteNodes进行碰撞。我遵循了一个教程,但它对我不起作用。

当我运行游戏时,两个对象发生碰撞,输出监视器中没有println("beginContact"),因此不会调用didBeginContact函数。

override init(size:CGSize) {
    super.init(size: size)
    player.planeSprite = SKSpriteNode(imageNamed: "plane5")
    player.planeSprite.physicsBody? = SKPhysicsBody(rectangleOfSize: player.planeSprite.size)
    player.planeSprite.physicsBody?.dynamic = true;
    player.planeSprite.physicsBody?.categoryBitMask = playerCategory;
    player.planeSprite.physicsBody?.contactTestBitMask = noteCategory;
    player.planeSprite.physicsBody?.collisionBitMask = 0;
    player.planeSprite.physicsBody?.usesPreciseCollisionDetection = true;
    player.planeSprite.position = CGPointMake(player.legalPositions[1], player.planeSprite.size.height/2)
    self.addChild(player.planeSprite)
}
func addNote() {
    var note:SKSpriteNode = SKSpriteNode(imageNamed: "note")
    note.physicsBody? = SKPhysicsBody(rectangleOfSize: note.size)
    note.physicsBody?.dynamic = true
    note.physicsBody?.categoryBitMask = noteCategory
    note.physicsBody?.contactTestBitMask = playerCategory
    note.physicsBody?.collisionBitMask = 0
    let minX = note.size.width/2
    let maxX = self.frame.size.width - note.size.width/2
    let rangeX = maxX - minX
    let position = Int(arc4random_uniform(3))
    note.position = CGPointMake(player.legalPositions[position], self.frame.size.height + note.size.height)
    self.addChild(note)
}
func didBeginContact(contact: SKPhysicsContact!) {
    println("beginContact")
    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 & noteCategory) != 0 && (secondBody.categoryBitMask & playerCategory) != 0 ) {
        println("if statement")
        playerDidCollideWithNote(firstBody.node as SKSpriteNode, note: secondBody.node as SKSpriteNode)
    }
}
func playerDidCollideWithNote(plane:SKSpriteNode, note:SKSpriteNode) {
    println("hit")
    note.removeFromParent()
}

如果使用Xcode8.0 Swift3.0,则不能使用
func didBeginContact(contact: SKPhysicsContact!) {}

所以你应该用这个。func didBegin(_ contact: SKPhysicsContact) {}

多次犯此错误。

您首先需要从SKPhysicsContactDelegate:继承

class GameScene: SKScene, SKPhysicsContactDelegate { ... }

然后在您的didMoveToView方法中,添加:

override func didMoveToView(view: SKView) {
    self.physicsWorld.contactDelegate = self
}

将您的GameScene实例设置为physicsWorld的contactDelegate。

从文档中,

当两个物理体重叠时,实现SKPhysicsContactDelegate协议的对象可以响应contactTestBitMask值在物理中相互接触世界要接收联系信息,您可以设置contactDelegateSKPhysicsWorld对象的属性。当接触开始或结束。

因此,您需要采用SKPhysicsContactDelegate协议,并通过将physicsWorldcontactDelegate属性设置为SKScene

class GameScene: SKScene, SKPhysicsContactDelegate {
    override func didMove(to view: SKView) {
        physicsWorld.contactDelegate = self
    }
}

此外,您应该删除?

player.planeSprite.physicsBody? = SKPhysicsBody(rectangleOfSize: player.planeSprite.size)
note.physicsBody? = SKPhysicsBody(rectangleOfSize: note.size)

最新更新