物理联系代表不工作swift 3 xCode 8

时间:2017-06-05 15:31:19

标签: swift xcode sprite-kit

无论我做什么,我似乎无法使用我的didBegin联系功能,我的spriteNodes设置为动态,我非常确定我的categoryBitMasks和contactTestBitMasks是正确的......这里&#39 ;是我的代码的物理部分。我的打印命令都没有从我的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语句,当机构联系时触发了代码,但现在的问题是,我无法对这些联系人之间的身体进行排序。

1 个答案:

答案 0 :(得分:1)

如果您使用简单类别,每个物理主体只属于一个类别,则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的类别bitMask,则将fallersNode设置为bodyA,否则将fallersNode设置为bodyB。