碰撞检测不止一次

时间:2015-08-18 02:33:39

标签: swift sprite-kit skspritenode

我正在做一个射击游戏我到目前为止的问题是当子弹与敌人碰撞并调用功能更新分数时,他们会调用该函数大约10次,继承人我的didBeginContact:

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 == CollisionCategories.Bullet) &&
        (secondBody.categoryBitMask == CollisionCategories.Enemy) {

            println("Hit")
            firstBody.node!.removeFromParent()
            secondBody.node!.removeFromParent()
            //change score
            updateScore(1)

    }
}

1 个答案:

答案 0 :(得分:0)

我遇到的问题是Enemy将其physicsBody配置为大小纹理,通过将其从纹理更改为circleOfRadius,工作正常。

自:

Enemy.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())

要:

Enemy.physicsBody = SKPhysicsBody(circleOfRadius: (Enemy.size.width/2))