为什么这些碰撞没有感觉? (Swift 雪碧套件)

Why won't these collisions sense? (Swift SpriteKit)

在我的项目中,我试图检测两个 SKSpriteNode 之间的碰撞,但它不起作用。 这是我的物理类别代码 :

struct PhysicsCategory {
static let enemy : UInt32 = 0x1 << 1
static let player : UInt32 = 0x1 << 3
static let neutral : UInt32 = 0x1 << 2
 }

对于中立体、敌人和玩家体

  neutral = SKSpriteNode(imageNamed: "Cave-Bottom")
    neutral.size = CGSize(width: self.frame.width, height: self.frame.height / 8)
    neutral.position = CGPoint(x: 0, y: self.frame.height / -2.5)
    neutral.zPosition = 2
    neutral.physicsBody?.categoryBitMask = PhysicsCategory.neutral
    neutral.physicsBody?.collisionBitMask = PhysicsCategory.player
    neutral.physicsBody?.contactTestBitMask = PhysicsCategory.player
    neutral.physicsBody = SKPhysicsBody.init(rectangleOf:              neutral.size)

    enemy = SKSpriteNode(imageNamed: "spike")
    enemy.size = CGSize(width: 40, height: 40)
    enemy.position = CGPoint(x: 0, y: 0)
    enemy.zPosition = 2
    enemy.physicsBody = SKPhysicsBody.init(rectangleOf: enemy.size)
    enemy.physicsBody?.categoryBitMask = PhysicsCatagory.enemy
    enemy.physicsBody?.collisionBitMask = PhysicsCatagory.player
    enemy.physicsBody?.contactTestBitMask = PhysicsCatagory.player

    player = SKSpriteNode(imageNamed: "bob")
    player.size = CGSize(width: 40, height: 40)
    bob.zPosition = 3
    player.physicsBody?.categoryBitMask = PhysicsCatagory.player
    player.physicsBody?.collisionBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral
    player.physicsBody?.contactTestBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral
    player.physicsBody = SKPhysicsBody.init(rectangleOf: player.size)

以及我的 ContactHasBegan 函数的代码:

  func didBegin(_ contact: SKPhysicsContact) {

   print("collided!")

    if (contact.bodyA.categoryBitMask == PhysicsCatagory.enemy && contact.bodyB.categoryBitMask == PhysicsCatagory.player) {
        print("player and enemy!")
    }
    if (contact.bodyB.categoryBitMask == PhysicsCatagory.enemy && contact.bodyA.categoryBitMask == PhysicsCatagory.player) {
        print("player and enemy!")
    }
    if (contact.bodyA.categoryBitMask == PhysicsCatagory.neutral && contact.bodyB.categoryBitMask == PhysicsCatagory.player) {
        print("neutral and player!")    
    }
    if contact.bodyB.categoryBitMask == PhysicsCatagory.neutral && contact.bodyA.categoryBitMask == PhysicsCatagory.player{
        print("neutral and player")
    }          }

出于某种原因,它只检测玩家和敌人之间的碰撞,并打印 didBeginContact 函数中 if 语句的 "Collided!" 和 none 测试结果为阳性。

因为播放器的 physicsBody 没有正确设置 categorycontactTest 位掩码。

您在尝试设置玩家的 physicsBody 属性后创建了玩家的 physicsBody:

player.physicsBody?.categoryBitMask = PhysicsCatagory.player
player.physicsBody?.collisionBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral
player.physicsBody?.contactTestBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral
player.physicsBody = SKPhysicsBody.init(rectangleOf: player.size)

在前 3 行代码中,因为 player.physicsBody 是可选的,所以该行的其余部分将被忽略,因为没有 physicsBody。

因此创建的物理体的所有属性都设置为默认值,即它与所有物体碰撞但不接触任何物体。

您需要移动线路:

player.physicsBody = SKPhysicsBody.init(....

到设置物理体属性的代码之前:

player.physicsBody = SKPhysicsBody.init(rectangleOf: player.size)
player.physicsBody?.categoryBitMask = PhysicsCatagory.player
player.physicsBody?.collisionBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral
player.physicsBody?.contactTestBitMask = PhysicsCatagory.enemy | PhysicsCatagory.neutral