我有三个物理类别:英雄,地面和墙壁。我的问题是地面的物理类别没有得到认可。为了解决这个问题,当英雄与墙壁和地面碰撞时,我打印了位掩码。 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!")
}
答案 0 :(得分:1)
您尝试设置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
}