无法检测到spritekit物理碰撞

时间:2017-02-04 22:34:04

标签: sprite-kit game-physics

我有4个物理机构,它们都能很好地检测到碰撞。但是,有两个物理实体无法检测它们何时相互碰撞。他们确实检测到他们何时与其他物理机构碰撞。我有所有他们的contacttestbitmas所以​​我不明白为什么有问题。这是一些代码: 这是我设置物理机构的地方:

    struct PhysicsCategory{
    static let None     : UInt32 = 0
    static let All      : UInt32 = UInt32.max
    static let player   : UInt32 = 0b1
    static let bounce   : UInt32 = 0b10
    static let blueball : UInt32 = 0b100
    static let redball  : UInt32 = 0b1000
    static let coin     : UInt32 = 0b10000
}

这是我用来设置播放器物理主体的代码,这是一个有问题的物理实体:

     player.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: player.size.width-40, height: player.size.height-40))
    player.physicsBody?.isDynamic = true
    player.physicsBody?.categoryBitMask = PhysicsCategory.player
    player.physicsBody?.contactTestBitMask = PhysicsCategory.blueball
    player.physicsBody?.contactTestBitMask = PhysicsCategory.redball
    player.physicsBody?.collisionBitMask = PhysicsCategory.None

这是用于检测碰撞的函数:

  func didBegin(_ 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 & PhysicsCategory.player != 0) &&
        (secondBody.categoryBitMask & PhysicsCategory.redball != 0)) {
        RedballDidCollideWithPlayer(player: firstBody.node as! SKSpriteNode, redball:  secondBody.node as! SKSpriteNode)
    }
    if ((firstBody.categoryBitMask & PhysicsCategory.player != 0) &&
        (secondBody.categoryBitMask & PhysicsCategory.blueball != 0)) {
        BlueballDidCollideWithPlayer(player: firstBody.node as! SKSpriteNode, blueball:  secondBody.node as! SKSpriteNode)
    }
    if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) &&
        (secondBody.categoryBitMask & PhysicsCategory.redball != 0)){
        RedballDidCollideWithBounce(bounce: firstBody.node as! SKSpriteNode, redball: secondBody.node as! SKSpriteNode)
    }
    if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) &&
        (secondBody.categoryBitMask & PhysicsCategory.blueball != 0)) {
        BlueballDidCollideWithBounce(bounce: firstBody.node as! SKSpriteNode, blueball: secondBody.node as! SKSpriteNode)
    }
    if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) &&
        (secondBody.categoryBitMask & PhysicsCategory.coin != 0)) {
        coinDidCollideWithplayer(player: firstBody.node as! SKSpriteNode, coin: secondBody.node as! SKSpriteNode)
    }
    if ((firstBody.categoryBitMask & PhysicsCategory.player != 0) &&
        (secondBody.categoryBitMask & PhysicsCategory.coin != 0)) {
        coinDidCollideWithplayer(player: firstBody.node as!        SKSpriteNode, coin: secondBody.node as! SKSpriteNode)
    }

}

以下是我用于设置蓝球的代码。这是另一个有问题的物理机构:

    let blueball = SKSpriteNode(imageNamed: "blueball")
        blueball.position = enemyb.position
        blueball.physicsBody = SKPhysicsBody(circleOfRadius: blueball.size.width/2)
        blueball.physicsBody?.isDynamic = true
        blueball.physicsBody?.categoryBitMask = PhysicsCategory.blueball
        blueball.physicsBody?.contactTestBitMask = PhysicsCategory.player
        blueball.physicsBody?.contactTestBitMask = PhysicsCategory.bounce
        blueball.physicsBody?.collisionBitMask = PhysicsCategory.None
        blueball.physicsBody?.usesPreciseCollisionDetection = true
        addChild(blueball)
        let actionMove = SKAction.move(to: player.position, duration: 2)

这里的任何想法都会有所帮助。我一直试图找到问题几天没有运气。

1 个答案:

答案 0 :(得分:1)

设置多个类别时,您必须一起这些值。你的代码

player.physicsBody?.contactTestBitMask = PhysicsCategory.blueball
player.physicsBody?.contactTestBitMask = PhysicsCategory.redball

只需将类别设置两次,第二次覆盖第一个类别。将其更改为:

player.physicsBody?.contactTestBitMask = PhysicsCategory.blueball | PhysicsCategory.redball
相关问题