我有三个物理类别:英雄,地面和墙壁。我的问题是地面的物理类别没有被认可。为了解决这个问题,我打印了当英雄与墙壁和地面相撞时的位面具。Wall按预期工作,并显示其位掩码2。然而,地面显示4294967295。(应该是4)地面有一个边缘物理体,它起作用是因为英雄没有穿过它,只是没有被认作地面。
物理范畴

enum PhysicsCategory:UInt32
{
    case hero = 1
    case wall = 2
    case ground = 4
}

地面等级:
class Ground: SKSpriteNode
{
    var groundTexture = SKTexture(imageNamed: "ground4")
    var jumpWidth = CGFloat()
    var jumpCount = CGFloat(1)

    func spawn(parentNode: SKNode, position: CGPoint, size:CGSize)
    {
        parentNode.addChild(self)
        self.size = size
        self.position = position
        self.zPosition = 2
        self.anchorPoint = CGPointMake(0, 1)
        self.texture = SKTexture(imageNamed: "ground4")
        self.physicsBody?.categoryBitMask = PhysicsCategory.ground.rawValue
        self.physicsBody?.affectedByGravity = false
        self.physicsBody?.dynamic = false

        let pointTopRight = CGPoint(x: size.width, y: 0)
        self.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero, toPoint: pointTopRight)
    }

didMoveToView:
let groundPosition = CGPoint(x: -self.size.width, y: 30)
let groundSize = CGSize(width: self.size.width * 3, height: 0)
ground.spawn(world, position: groundPosition, size: groundSize)

didBeginContact公司
let firstBody = contact.bodyA
let secondBody = contact.bodyB

if firstBody.categoryBitMask == PhysicsCategory.hero.rawValue && secondBody.categoryBitMask == PhysicsCategory.ground.rawValue || firstBody.categoryBitMask == PhysicsCategory.ground.rawValue && secondBody.categoryBitMask == PhysicsCategory.hero.rawValue
        {
            print("contact with the ground!")
        }

最佳答案

在尝试设置categoryBitMask之后,实际上是在创建physicsBody。所以你试图将categoryBitMask设置为nil。。。
你只需要把那一排移到。。。

func spawn(parentNode: SKNode, position: CGPoint, size:CGSize)
{
    parentNode.addChild(self)
    self.size = size
    self.position = position
    self.zPosition = 2
    self.anchorPoint = CGPointMake(0, 1)
    self.texture = SKTexture(imageNamed: "ground4")
    let pointTopRight = CGPoint(x: size.width, y: 0)

    self.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero, toPoint: pointTopRight)

    self.physicsBody?.categoryBitMask = PhysicsCategory.ground.rawValue
    self.physicsBody?.affectedByGravity = false
    self.physicsBody?.dynamic = false
}

10-05 18:22