didBeginContact未调用

时间:2014-10-30 13:16:52

标签: swift sprite-kit collision

我正在制作一个游戏,我需要让我的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()
}

3 个答案:

答案 0 :(得分:12)

多次完成这个错误。

您首先需要继承SKPhysicsContactDelegate:

class GameScene: SKScene, SKPhysicsContactDelegate { ... }

然后在didMoveToView方法中添加:

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

将GameScene实例设置为physicsWorld的contactDelegate。

答案 1 :(得分:10)

如果您使用Xcode8.0 Swift3.0,则无法使用
var peer = new Peer('sane-03', {key: '97za6osy6ulkgldi'}); var conn = peer.connect('sanen-02'); if (conn) { conn.on('open', function(){ conn.send('hi, connection opened!'); }); } document.getElementById("1").onclick = function() { conn.send('Hello again'); };

所以你应该使用它。 func didBeginContact(contact: SKPhysicsContact!) {}

答案 2 :(得分:-1)

来自文档,

  

实现SKPhysicsContactDelegate协议的对象可以在两个物理实体重叠时做出响应   contactTestBitMask值在物理学中相互接触   世界。要接收联系人消息,请设置contactDelegate   SKPhysicsWorld对象的属性。代表在a时被调用   联系开始或结束。

因此,您需要采用SKPhysicsContactDelegate协议并通过

physicsWorld的{​​{1}}属性设置为SKScene
contactDelegate

另外,你应该删除?来自以下陈述

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