【问题标题】:collisionBitMask values not working in SpriteKit碰撞位掩码值在 SpriteKit 中不起作用
【发布时间】:2017-08-04 15:07:29
【问题描述】:

我有两个想要相互碰撞的实体。我有一个结构来跟踪不同的物理类别:

struct PhysicsCategory {
    static let Player: Int32 = 0x1 << 1
    static let Obstacle: Int32 = 0x1 << 2
    static let Ground: Int32 = 0x1 << 3
}

我希望我的 Player 节点与我的 Obstacle 节点发生冲突。这是我的 Player 节点的物理特性:

    self.physicsBody = SKPhysicsBody(rectangleOf: (self.size))
    self.physicsBody?.isDynamic = true
    self.physicsBody?.categoryBitMask = UInt32(PhysicsCategory.Player)
    self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Obstacle)
    self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground)

这是我的障碍节点的物理原理

    self.physicsBody = SKPhysicsBody(rectangleOf: self.size)
    self.physicsBody?.categoryBitMask = UInt32(PhysicsCategory.Obstacle)
    self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Player)
    self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground)
    self.physicsBody?.isDynamic = true
    self.physicsBody?.velocity = CGVector(dx: -240, dy: 0)
    self.physicsBody?.linearDamping = 0
    self.physicsBody?.friction = 0

当他们穿越路径时,他们只是互相穿过。但是,它们都正确地与地面相撞。我做错了什么?

【问题讨论】:

  • 编程的基本原理告诉你,如果你写 x = 1 并且在 x = 2 之后,你的 x 是 2

标签: swift sprite-kit skphysicsbody


【解决方案1】:

当您第二次设置 collisionBitMask 时,您将覆盖它。您应该使用符号| 设置OR 位掩码。

替换

self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Obstacle)
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground)

与:

self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Obstacle) | UInt32(PhysicsCategory.Ground)

self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Player)
self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Ground)

与:

self.physicsBody?.collisionBitMask = UInt32(PhysicsCategory.Player) | UInt32(PhysicsCategory.Ground)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多