无论我做什么,我似乎都无法使didBegin联系人功能正常工作,我的spriteNodes设置为dynamic,我非常确定我的categoryBitMasks和contactTestBitMasks是正确的。。。这是我代码的物理部分。我的打印命令都不是从我的didBeginContact函数打印的。

class GameScene: SKScene, SKPhysicsContactDelegate {

var theLine = SKSpriteNode()
var fallers = SKSpriteNode()

enum colliderType: UInt32 {

    case theLine = 1
    case fallers = 2
}

override func sceneDidLoad() {

self.physicsWorld.contactDelegate = self

    theLine = SKSpriteNode()
    theLine.zPosition = 0
    theLine.size = CGSize(width: 0, height: screenWidth / 128
    theLine.position = CGPoint(x: midX, y: midY)
    theLine.physicsBody = SKPhysicsBody(rectangleOf: theLine.size)
    theLine.physicsBody?.isDynamic = true
    theLine.physicsBody?.affectedByGravity = false
    theLine.physicsBody?.allowsRotation = false
    theLine.physicsBody?.categoryBitMask = colliderType.theLine.rawValue
    theLine.physicsBody?.contactTestBitMask = colliderType.fallers.rawValue
    theLine.physicsBody?.collisionBitMask = 0
    addChild(theLine)

fallers[i].size = CGSize(width: 75, height: 75)
fallers[i].color = UIColor.white
fallers[i].alpha = 1
fallers[i].setScale(ds * 4)
fallers[i].zPosition = 9
let fallerTexture = fallers[i].texture
fallers[i].physicsBody = SKPhysicsBody(texture: fallerTexture!, size:    fallers[i].size)
fallers[i].physicsBody!.isDynamic = true
fallers[i].physicsBody!.categoryBitMask = colliderType.fallers.rawValue
fallers[i].physicsBody!.contactTestBitMask = colliderType.theLine.rawValue
fallers[i].physicsBody!.collisionBitMask = 0
fallers[i].physicsBody!.affectedByGravity = false

addChild(fallers[i])

}



func didBegin(_ contact: SKPhysicsContact) {


//Detects collisions and what nodes the collisions are between.


if contact.bodyA.categoryBitMask == colliderType.theLine.rawValue && contact.bodyB.categoryBitMask == colliderType.fallers.rawValue {

    print("CONTACT")

} else if contact.bodyA.categoryBitMask == colliderType.fallers.rawValue && contact.bodyB.categoryBitMask == colliderType.theLine.rawValue {

    print("CONTACT")

    }

}

更新
我刚刚在我的didBeginContactDelegate中放了一个else语句,代码是在身体接触时触发的,但现在的问题是,我无法对这些接触之间的身体进行排序。

最佳答案

如果您使用的是简单的类别,每个物理体只属于一个类别,那么这种didBeginContact的替代形式可能更具可读性:

func didBeginContact(contact: SKPhysicsContact) {
    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    switch contactMask {

    case colliderTytpe.fallers.rawValue | colliderType.theLine.rawValue:
        print("Collision between fallers and theLine")
        let fallersNode = contact.bodyA.categoryBitMask == colliderTytpe.fallers.rawValue ? contact.bodyA.node! : contact.bodyB.node!

        // Do something with fallersNode
        fallersNode.removeFromParent() // For example
        score += 10 // For example

    default :
        //Some other contact has occurred
        print("Some other contact")
    }
}

(如果需要处理其他冲突,只需向开关添加更多大小写块)。
它避免了混淆bodyA和bodyB,并将它们排序。您只需要在必须对其中一个节点执行特定操作时处理它们,这就是这行代码的作用:
let fallersNode = contact.bodyA.categoryBitMask == colliderTytpe.fallers.rawValue ? contact.bodyA.node! : contact.bodyB.node!

它使用Swift的三元运算符来表示“如果bodyA具有faller的category位掩码,则将fallersNode设置为bodyA,否则将fallersNode设置为bodyB。

10-08 11:42