如何用SKShapenode创建墙?

时间:2015-12-04 00:18:33

标签: ios swift skphysicsbody skshapenode

我正在尝试使用SKShapenode在Swift中创建墙。即使将碰撞位掩码设置为“全部”,字符仍会在矩形中移动,不会与其交互。

设置物理类别:

struct PhysicsCategory {
    static let None      : UInt32 = 0
    static let All       : UInt32 = UInt32.max
    static let Monster   : UInt32 = 0b1
    static let Projectile: UInt32 = 0b10
    static let Walls     : UInt32 = 0b11
    static let Player    : UInt32 = 0b100
}

我是如何设置墙壁的:

let rectangle = SKShapeNode(rectOfSize: CGSize(width:390, height:200))
rectangle.position = CGPointMake(frame.midX-10, frame.midY + 50)
rectangle.strokeColor = SKColor.blackColor()
rectangle.glowWidth = 1.0

rectangle.physicsBody = SKPhysicsBody(edgeLoopFromRect: rectangle.frame)
rectangle.physicsBody?.dynamic = false
rectangle.physicsBody?.categoryBitMask = PhysicsCategory.Walls
rectangle.physicsBody?.contactTestBitMask = PhysicsCategory.None
rectangle.physicsBody?.collisionBitMask = PhysicsCategory.All
self.addChild(rectangle)

2 个答案:

答案 0 :(得分:0)

您必须将contactTestBitMask设置为'全部'不是collisionBitMask

rectangle.physicsBody.contactTestBitMask = PhysicsCategory.All

答案 1 :(得分:0)

角色的设置是什么?他们的物理实体也必须正确设置才能发挥作用。

此外,至少有一个SKNode的physicsBody必须设置为dynamic = true,如果两者都有dynamic = false,那么它将无法正常工作。